CGObjCGNU.cpp revision fc8f0e14ad142ed811e90fbd9a30e419e301c717
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"
18dce1406f1c1f572cfd61c494546572d63461c741Chris Lattner#include "CodeGenModule.h"
198f2926b73ed635afecd020da787af6a837601a2bDaniel Dunbar#include "CodeGenFunction.h"
2036f893c1efe367f929d92c8b125f964c22ba189eJohn McCall#include "CGCleanup.h"
215dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
22dce1406f1c1f572cfd61c494546572d63461c741Chris Lattner#include "clang/AST/ASTContext.h"
23e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Decl.h"
24af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "clang/AST/DeclObjC.h"
2519cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
2616f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall#include "clang/Basic/SourceManager.h"
289f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall#include "clang/Basic/FileManager.h"
295dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
305dc0867af17b3bd6f567897433853b2b4767446cChris Lattner#include "llvm/Intrinsics.h"
310f984268b05edab2cc555a427c441baa9c252658Chris Lattner#include "llvm/Module.h"
32c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall#include "llvm/LLVMContext.h"
330f984268b05edab2cc555a427c441baa9c252658Chris Lattner#include "llvm/ADT/SmallVector.h"
3420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/ADT/StringMap.h"
3580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall#include "llvm/Support/CallSite.h"
367ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar#include "llvm/Support/Compiler.h"
377ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar#include "llvm/Target/TargetData.h"
385dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
3920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include <map>
409f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall#include <stdarg.h>
41e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
42e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
43dce1406f1c1f572cfd61c494546572d63461c741Chris Lattnerusing namespace clang;
4446f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbarusing namespace CodeGen;
4520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovusing llvm::dyn_cast;
4620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
470f984268b05edab2cc555a427c441baa9c252658Chris Lattner
480f984268b05edab2cc555a427c441baa9c252658Chris Lattnernamespace {
4981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class that lazily initialises the runtime function.  Avoids inserting the
5081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// types and the function declaration into a module if they're not used, and
5181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// avoids constructing the type more than once if it's used more than once.
529f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass LazyRuntimeFunction {
539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CodeGenModule *CGM;
549f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::vector<const llvm::Type*> ArgTys;
559f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const char *FunctionName;
569f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::Function *Function;
579f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
5881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Constructor leaves this class uninitialized, because it is intended to
5981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// be used as a field in another class and not all of the types that are
6081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// used as arguments will necessarily be available at construction time.
619f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    LazyRuntimeFunction() : CGM(0), FunctionName(0), Function(0) {}
629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
6381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Initialises the lazy function with the name, return type, and the types
6481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// of the arguments.
659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    END_WITH_NULL
669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    void init(CodeGenModule *Mod, const char *name,
679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        const llvm::Type *RetTy, ...) {
689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       CGM =Mod;
699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       FunctionName = name;
709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       Function = 0;
719735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       ArgTys.clear();
729f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_list Args;
739f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_start(Args, RetTy);
749f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall         while (const llvm::Type *ArgTy = va_arg(Args, const llvm::Type*))
759f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall           ArgTys.push_back(ArgTy);
769f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_end(Args);
779f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       // Push the return type on at the end so we can pop it off easily
789f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.push_back(RetTy);
799f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall   }
8081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall   /// Overloaded cast operator, allows the class to be implicitly cast to an
8181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall   /// LLVM constant.
829f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall   operator llvm::Function*() {
839f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     if (!Function) {
849735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       if (0 == FunctionName) return 0;
859735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // We put the return type on the end of the vector, so pop it back off
869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       const llvm::Type *RetTy = ArgTys.back();
879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.pop_back();
889f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       llvm::FunctionType *FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
899f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       Function =
909f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall         cast<llvm::Function>(CGM->CreateRuntimeFunction(FTy, FunctionName));
919735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // We won't need to use the types again, so we may as well clean up the
929735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // vector now
939f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.resize(0);
949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     }
959f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     return Function;
969f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall   }
979f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
10081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// GNU Objective-C runtime code generation.  This class implements the parts of
10181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Objective-C support that are specific to the GNU family of runtimes (GCC and
10281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// GNUstep).
1039f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGNU : public CGObjCRuntime {
104c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprotected:
10581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The module that is using this class
1069f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CodeGenModule &CGM;
10781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The LLVM module into which output is inserted
1080f984268b05edab2cc555a427c441baa9c252658Chris Lattner  llvm::Module &TheModule;
10981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// strut objc_super.  Used for sending messages to super.  This structure
11081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// contains the receiver (object) and the expected class.
111c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  const llvm::StructType *ObjCSuperTy;
11281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// struct objc_super*.  The type of the argument to the superclass message
11381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// lookup functions.
114c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  const llvm::PointerType *PtrToObjCSuperTy;
11581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for selectors.  Opaque pointer (i8*) unless a header declaring
11681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// SEL is included in a header somewhere, in which case it will be whatever
11781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// type is declared in that header, most likely {i8*, i8*}.
118e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::PointerType *SelectorTy;
11981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM i8 type.  Cached here to avoid repeatedly getting it in all of the
12081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// places where it's used
121d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  const llvm::IntegerType *Int8Ty;
12281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Pointer to i8 - LLVM type of char*, for all of the places where the
12381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime needs to deal with C strings.
124e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::PointerType *PtrToInt8Ty;
12581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Instance Method Pointer type.  This is a pointer to a function that takes,
12681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// at a minimum, an object and a selector, and is the generic type for
12781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C methods.  Due to differences between variadic / non-variadic
12881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// calling conventions, it must always be cast to the correct type before
12981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// actually being used.
130c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  const llvm::PointerType *IMPTy;
13181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Type of an untyped Objective-C object.  Clang treats id as a built-in type
13281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
13381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// but if the runtime header declaring it is included then it may be a
13481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pointer to a structure.
135e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::PointerType *IdTy;
13681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Pointer to a pointer to an Objective-C object.  Used in the new ABI
13781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// message lookup function and some GC-related functions.
138ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  const llvm::PointerType *PtrToIdTy;
13981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The clang type of id.  Used when using the clang CGCall infrastructure to
14081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// call Objective-C methods.
141ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType ASTIdTy;
14281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C int type.
143e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::IntegerType *IntTy;
14481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for an opaque pointer.  This is identical to PtrToInt8Ty, but is
14581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used in the code to document the difference between i8* meaning a pointer
14681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// to a C string and i8* meaning a pointer to some opaque type.
147e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::PointerType *PtrTy;
14881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C long type.  The runtime uses this in a lot of places where
14981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// it should be using intptr_t, but we can't fix this without breaking
15081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// compatibility with GCC...
151e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::IntegerType *LongTy;
15281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C size_t.  Used in various runtime data structures.
1538fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  const llvm::IntegerType *SizeTy;
15481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C ptrdiff_t.  Mainly used in property accessor functions.
1558fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  const llvm::IntegerType *PtrDiffTy;
15681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C int*.  Used for GCC-ABI-compatible non-fragile instance
15781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// variables.
158e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::PointerType *PtrToIntTy;
15981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for Objective-C BOOL type.
1608fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  const llvm::Type *BoolTy;
16181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Metadata kind used to tie method lookups to message sends.  The GNUstep
16281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime provides some LLVM passes that can use this to do things like
16381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// automatic IMP caching and speculative inlining.
164c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  unsigned msgSendMDKind;
16581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Helper function that generates a constant string and returns a pointer to
16681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// the start of the string.  The result of this function can be used anywhere
16781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// where the C code specifies const char*.
1689735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  llvm::Constant *MakeConstantString(const std::string &Str,
1699735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                     const std::string &Name="") {
1709735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
1719735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
1729735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
17381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Emits a linkonce_odr string, whose name is the prefix followed by the
17481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// string value.  This allows the linker to combine the strings between
17581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// different modules.  Used for EH typeinfo names, selector strings, and a
17681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// few other things.
1779735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  llvm::Constant *ExportUniqueString(const std::string &Str,
1789735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                     const std::string prefix) {
1799735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    std::string name = prefix + Str;
1809735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
1819735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    if (!ConstStr) {
1829735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true);
1839735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
1849735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall              llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
1859735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    }
1869735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
1879735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
18881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global structure, initialized by the elements in the vector.
18981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The element types must match the types of the structure elements in the
19081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// first argument.
191c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::GlobalVariable *MakeGlobal(const llvm::StructType *Ty,
1929735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   std::vector<llvm::Constant*> &V,
1939735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   llvm::StringRef Name="",
1949735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   llvm::GlobalValue::LinkageTypes linkage
1959735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
1969735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
1979735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return new llvm::GlobalVariable(TheModule, Ty, false,
1989735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        linkage, C, Name);
1999735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
20081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global array.  The vector must contain the same number of
20181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// elements that the array type declares, of the type specified as the array
20281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// element type.
203c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::GlobalVariable *MakeGlobal(const llvm::ArrayType *Ty,
2049735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   std::vector<llvm::Constant*> &V,
2059735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   llvm::StringRef Name="",
2069735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   llvm::GlobalValue::LinkageTypes linkage
2079735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
2089735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
2099735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return new llvm::GlobalVariable(TheModule, Ty, false,
2109735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                    linkage, C, Name);
2119735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
21281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global array, inferring the array type from the specified
21381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// element type and the size of the initialiser.
214c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::GlobalVariable *MakeGlobalArray(const llvm::Type *Ty,
2159735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                        std::vector<llvm::Constant*> &V,
2169735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                        llvm::StringRef Name="",
2179735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                        llvm::GlobalValue::LinkageTypes linkage
2189735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
2199735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size());
2209735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return MakeGlobal(ArrayTy, V, Name, linkage);
2219735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
22281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Ensures that the value has the required type, by inserting a bitcast if
22381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// required.  This function lets us avoid inserting bitcasts that are
22481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// redundant.
225c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Value* EnforceType(CGBuilderTy B, llvm::Value *V, const llvm::Type *Ty){
226c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    if (V->getType() == Ty) return V;
227c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    return B.CreateBitCast(V, Ty);
228c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
229c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // Some zeros used for GEPs in lots of places.
230c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Constant *Zeros[2];
23181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Null pointer value.  Mainly used as a terminator in various arrays.
232c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Constant *NULLPtr;
23381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM context.
234c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::LLVMContext &VMContext;
235c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprivate:
23681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Placeholder for the class.  Lots of things refer to the class before we've
23781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// actually emitted it.  We use this alias as a placeholder, and then replace
23881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// it with a pointer to the class structure before finally emitting the
23981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// module.
2405efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  llvm::GlobalAlias *ClassPtrAlias;
24181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Placeholder for the metaclass.  Lots of things refer to the class before
24281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// we've / actually emitted it.  We use this alias as a placeholder, and then
24381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// replace / it with a pointer to the metaclass structure before finally
24481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// emitting the / module.
2455efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  llvm::GlobalAlias *MetaClassPtrAlias;
24681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the classes that have been generated for this compilation units.
24720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Classes;
24881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the categories that have been generated for this compilation units.
24920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Categories;
25081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the Objective-C constant strings that have been generated for this
25181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// compilation units.
25220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> ConstantStrings;
25381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Map from string values to Objective-C constant strings in the output.
25481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Used to prevent emitting Objective-C strings more than once.  This should
25581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// not be required at all - CodeGenModule should manage this list.
25648272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  llvm::StringMap<llvm::Constant*> ObjCStrings;
25781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the protocols that have been declared.
25820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::StringMap<llvm::Constant*> ExistingProtocols;
25981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// For each variant of a selector, we store the type encoding and a
26081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// placeholder value.  For an untyped selector, the type will be the empty
26181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// string.  Selector references are all done via the module's selector table,
26281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// so we create an alias as a placeholder and then replace it with the real
26381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// value later.
2649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
26581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Type of the selector map.  This is roughly equivalent to the structure
26681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used in the GNUstep runtime, which maintains a list of all of the valid
26781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// types for a selector in a table.
2689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  typedef llvm::DenseMap<Selector, llvm::SmallVector<TypedSelector, 2> >
2699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorMap;
27081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// A map from selectors to selector types.  This allows us to emit all
27181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// selectors of the same name and type together.
2729f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SelectorMap SelectorTable;
2739f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
27481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Selectors related to memory management.  When compiling in GC mode, we
27581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// omit these.
276ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  Selector RetainSel, ReleaseSel, AutoreleaseSel;
27781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Runtime functions used for memory management in GC mode.  Note that clang
27881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// supports code generation for calling these functions, but neither GNU
27981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime actually supports this API properly yet.
2809f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
2819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    WeakAssignFn, GlobalAssignFn;
2829f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
2839735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnallprotected:
28481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for throwing Objective-C exceptions.
2859f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction ExceptionThrowFn;
28681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for rethrowing exceptions, used at the end of @finally or
28781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// @synchronize blocks.
2889735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction ExceptionReThrowFn;
28981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when entering a catch function.  This is required for
29081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// differentiating Objective-C exceptions and foreign exceptions.
2919735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction EnterCatchFn;
29281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when exiting from a catch block.  Used to do exception
29381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// cleanup.
2949735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction ExitCatchFn;
29581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when entering an @synchronize block.  Acquires the lock.
2969f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SyncEnterFn;
29781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when exiting an @synchronize block.  Releases the lock.
2989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SyncExitFn;
2999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
3009735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnallprivate:
3019735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall
30281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called if fast enumeration detects that the collection is
30381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// modified during the update.
3049f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction EnumerationMutationFn;
30581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function for implementing synthesized property getters that return an
30681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// object.
3079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction GetPropertyFn;
30881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function for implementing synthesized property setters that return an
30981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// object.
3109f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SetPropertyFn;
31181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for non-object declared property getters.
3129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction GetStructPropertyFn;
31381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for non-object declared property setters.
3149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SetStructPropertyFn;
3159f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
31681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The version of the runtime that this class targets.  Must match the
31781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// version in the runtime.
3189f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const int RuntimeVersion;
31981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The version of the protocol class.  Used to differentiate between ObjC1
32081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and ObjC2 protocols.  Objective-C 1 protocols can not contain optional
32181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// components and can not contain declared properties.  We always emit
32281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C 2 property structures, but we have to pretend that they're
32381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C 1 property structures when targeting the GCC runtime or it
32481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// will abort.
3259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const int ProtocolVersion;
32620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovprivate:
32781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates an instance variable list structure.  This is a structure
32881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// containing a size and an array of structures containing instance variable
32981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// metadata.  This is used purely for introspection in the fragile ABI.  In
33081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// the non-fragile ABI, it's used for instance variable fixup.
33120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateIvarList(
33220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      const llvm::SmallVectorImpl<llvm::Constant *>  &IvarNames,
33320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      const llvm::SmallVectorImpl<llvm::Constant *>  &IvarTypes,
33420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      const llvm::SmallVectorImpl<llvm::Constant *>  &IvarOffsets);
33581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a method list structure.  This is a structure containing a size
33681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and an array of structures containing method metadata.
33781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  ///
33881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// This structure is used by both classes and categories, and contains a next
33981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pointer allowing them to be chained together in a linked list.
3409f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::Constant *GenerateMethodList(const llvm::StringRef &ClassName,
3419f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      const llvm::StringRef &CategoryName,
3421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const llvm::SmallVectorImpl<Selector>  &MethodSels,
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const llvm::SmallVectorImpl<llvm::Constant *>  &MethodTypes,
34420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      bool isClassMethodList);
34581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Emits an empty protocol.  This is used for @protocol() where no protocol
34681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// is found.  The runtime will (hopefully) fix up the pointer to refer to the
34781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// real protocol.
348f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
34981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a list of property metadata structures.  This follows the same
35081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pattern as method and instance variable metadata lists.
351d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
352d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian        llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
353d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian        llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
35481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a list of referenced protocols.  Classes, categories, and
35581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// protocols all use this structure.
35620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateProtocolList(
35720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      const llvm::SmallVectorImpl<std::string> &Protocols);
35881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// To ensure that all protocols are seen by the runtime, we add a category on
35981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// a class defined in the runtime, declaring no methods, but adopting the
36081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// protocols.  This is a horribly ugly hack, but it allows us to collect all
36181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// of the protocols without changing the ABI.
362d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  void GenerateProtocolHolderCategory(void);
36381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a class structure.
36420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateClassStructure(
36520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *MetaClass,
36620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *SuperClass,
36720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      unsigned info,
368d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner      const char *Name,
36920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *Version,
37020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *InstanceSize,
37120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *IVars,
37220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *Methods,
373d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *Protocols,
374d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *IvarOffsets,
3758c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall      llvm::Constant *Properties,
3768c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall      bool isMeta=false);
37781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a method list.  This is used by protocols to define the required
37881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and optional methods.
37920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateProtocolMethodList(
38020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      const llvm::SmallVectorImpl<llvm::Constant *>  &MethodNames,
38120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      const llvm::SmallVectorImpl<llvm::Constant *>  &MethodTypes);
38281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Returns a selector with the specified type encoding.  An empty string is
38381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used to return an untyped selector (with the types field set to NULL).
3849f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
3859f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    const std::string &TypeEncoding, bool lval);
38681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Returns the variable used to store the offset of an instance variable.
3879cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
3889cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      const ObjCIvarDecl *Ivar);
38981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Emits a reference to a class.  This allows the linker to object if there
39081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// is no class of the matching name.
3912a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  void EmitClassRef(const std::string &className);
392c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprotected:
39381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Looks up the method for sending a message to the specified object.  This
39481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// mechanism differs between the GCC and GNU runtimes, so this method must be
39581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// overridden in subclasses.
396c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
397c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *&Receiver,
398c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *cmd,
399c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::MDNode *node) = 0;
40081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Looks up the method for sending a message to a superclass.  This mechanism
40181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// differs between the GCC and GNU runtimes, so this method must be
40281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// overridden in subclasses.
403c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
404c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *ObjCSuper,
405c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *cmd) = 0;
4060f984268b05edab2cc555a427c441baa9c252658Chris Lattnerpublic:
4079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
4089f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      unsigned protocolClassVersion);
4099f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
4100d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  virtual llvm::Constant *GenerateConstantString(const StringLiteral *);
4119f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
4129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual RValue
4139f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GenerateMessageSend(CodeGenFunction &CGF,
414ef072fd2f3347cfd857d6eb787b245b950771430John McCall                      ReturnValueSlot Return,
4157f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                      QualType ResultType,
4167f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                      Selector Sel,
417f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                      llvm::Value *Receiver,
418df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                      const CallArgList &CallArgs,
419c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                      const ObjCInterfaceDecl *Class,
420df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                      const ObjCMethodDecl *Method);
4219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual RValue
4229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GenerateMessageSendSuper(CodeGenFunction &CGF,
423ef072fd2f3347cfd857d6eb787b245b950771430John McCall                           ReturnValueSlot Return,
4247f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                           QualType ResultType,
4257f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                           Selector Sel,
426f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                           const ObjCInterfaceDecl *Class,
4277ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian                           bool isCategoryImpl,
428f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                           llvm::Value *Receiver,
42919cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar                           bool IsClassMessage,
430d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                           const CallArgList &CallArgs,
431d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                           const ObjCMethodDecl *Method);
43245d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
433ddb2a3d55a24a1dbdf9152621642d9a4b4fc2f61Daniel Dunbar                                const ObjCInterfaceDecl *OID);
43403b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanian  virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
43503b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanian                                   bool lval = false);
4366d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbar  virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
4376d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbar      *Method);
4385a180397870944548aaadeaebf58e415885b9489John McCall  virtual llvm::Constant *GetEHType(QualType T);
4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
441679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian                                         const ObjCContainerDecl *CD);
4427ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
4437ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
44445d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
445af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar                                           const ObjCProtocolDecl *PD);
446af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
44720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  virtual llvm::Function *ModuleInitFunction();
44849f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar  virtual llvm::Function *GetPropertyGetFunction();
44949f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar  virtual llvm::Function *GetPropertySetFunction();
4508fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  virtual llvm::Function *GetSetStructFunction();
4518fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  virtual llvm::Function *GetGetStructFunction();
452309a4368cd299cff30ab22709e2b772cf8059e45Daniel Dunbar  virtual llvm::Constant *EnumerationMutationFunction();
4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4549f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitTryStmt(CodeGenFunction &CGF,
455f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                           const ObjCAtTryStmt &S);
4569f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitSynchronizedStmt(CodeGenFunction &CGF,
457f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                    const ObjCAtSynchronizedStmt &S);
4589f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitThrowStmt(CodeGenFunction &CGF,
45964d5d6c5903157c521af496479d06dc26032d718Anders Carlsson                             const ObjCAtThrowStmt &S);
4609f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
4616dc2317b59cb1180a59f6c283d96b7a5dfeb5307Fariborz Jahanian                                         llvm::Value *AddrWeakObj);
4629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitObjCWeakAssign(CodeGenFunction &CGF,
4633e283e344595e0bd499b13b30a92b7d9c10a2140Fariborz Jahanian                                  llvm::Value *src, llvm::Value *dst);
4649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitObjCGlobalAssign(CodeGenFunction &CGF,
465021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                    llvm::Value *src, llvm::Value *dest,
466021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                    bool threadlocal=false);
4679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitObjCIvarAssign(CodeGenFunction &CGF,
4686c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                    llvm::Value *src, llvm::Value *dest,
4696c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                    llvm::Value *ivarOffset);
4709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
47158626500527695865683d1d65053743de8770b60Fariborz Jahanian                                        llvm::Value *src, llvm::Value *dest);
4729f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitGCMemmoveCollectable(CodeGenFunction &CGF,
4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        llvm::Value *DestPtr,
474082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian                                        llvm::Value *SrcPtr,
47555bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian                                        llvm::Value *Size);
4769f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual LValue EmitObjCValueForIvar(CodeGenFunction &CGF,
477598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                      QualType ObjectTy,
478598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                      llvm::Value *BaseValue,
479598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                      const ObjCIvarDecl *Ivar,
480598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                      unsigned CVRQualifiers);
4819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
4822a03192a02dbf4fdff438d1e658356bde871aba4Daniel Dunbar                                      const ObjCInterfaceDecl *Interface,
483f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian                                      const ObjCIvarDecl *Ivar);
4849f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
4856b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                             const CGBlockInfo &blockInfo) {
48689ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian    return NULLPtr;
48789ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian  }
4880f984268b05edab2cc555a427c441baa9c252658Chris Lattner};
48981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class representing the legacy GCC Objective-C ABI.  This is the default when
49081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// -fobjc-nonfragile-abi is not specified.
49181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall///
49281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// The GCC ABI target actually generates code that is approximately compatible
49381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// with the new GNUstep runtime ABI, but refrains from using any features that
49481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// would not work with the GCC runtime.  For example, clang always generates
49581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// the extended form of the class structure, and the extra fields are simply
49681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// ignored by GCC libobjc.
4979f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGCC : public CGObjCGNU {
49881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The GCC ABI message lookup function.  Returns an IMP pointing to the
49981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// method implementation for this message.
500c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  LazyRuntimeFunction MsgLookupFn;
50181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The GCC ABI superclass message lookup function.  Takes a pointer to a
50281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// structure describing the receiver and the class, and a selector as
50381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// arguments.  Returns the IMP for the corresponding method.
504c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  LazyRuntimeFunction MsgLookupSuperFn;
505c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprotected:
506c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
507c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *&Receiver,
508c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *cmd,
509c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::MDNode *node) {
510c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGBuilderTy &Builder = CGF.Builder;
511c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Value *imp = Builder.CreateCall2(MsgLookupFn,
512c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall            EnforceType(Builder, Receiver, IdTy),
513c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall            EnforceType(Builder, cmd, SelectorTy));
514c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    cast<llvm::CallInst>(imp)->setMetadata(msgSendMDKind, node);
515c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    return imp;
516c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
517c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
518c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *ObjCSuper,
519c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *cmd) {
520c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
521c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
522c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          PtrToObjCSuperTy), cmd};
523c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      return Builder.CreateCall(MsgLookupSuperFn, lookupArgs, lookupArgs+2);
524c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
5259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
526c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
527c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // IMP objc_msg_lookup(id, SEL);
528c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, NULL);
529c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // IMP objc_msg_lookup_super(struct objc_super*, SEL);
530c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
531c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              PtrToObjCSuperTy, SelectorTy, NULL);
532c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
5339f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
53481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class used when targeting the new GNUstep runtime ABI.
5359f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGNUstep : public CGObjCGNU {
53681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// The slot lookup function.  Returns a pointer to a cacheable structure
53781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// that contains (among other things) the IMP.
538c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    LazyRuntimeFunction SlotLookupFn;
53981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// The GNUstep ABI superclass message lookup function.  Takes a pointer to
54081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// a structure describing the receiver and the class, and a selector as
54181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// arguments.  Returns the slot for the corresponding method.  Superclass
54281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// message lookup rarely changes, so this is a good caching opportunity.
543c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    LazyRuntimeFunction SlotLookupSuperFn;
54481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Type of an slot structure pointer.  This is returned by the various
54581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// lookup functions.
546c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Type *SlotTy;
547c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  protected:
548c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
549c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                   llvm::Value *&Receiver,
550c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                   llvm::Value *cmd,
551c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                   llvm::MDNode *node) {
552c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
553c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Function *LookupFn = SlotLookupFn;
554c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
555c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Store the receiver on the stack so that we can reload it later
556c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
557c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      Builder.CreateStore(Receiver, ReceiverPtr);
558c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
559c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *self;
560c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
561c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
562c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        self = CGF.LoadObjCSelf();
563c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      } else {
564c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        self = llvm::ConstantPointerNull::get(IdTy);
565c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      }
566c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
567c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // The lookup function is guaranteed not to capture the receiver pointer.
568c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      LookupFn->setDoesNotCapture(1);
569c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
570c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::CallInst *slot =
571c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          Builder.CreateCall3(LookupFn,
572c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              EnforceType(Builder, ReceiverPtr, PtrToIdTy),
573c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              EnforceType(Builder, cmd, SelectorTy),
574c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              EnforceType(Builder, self, IdTy));
575c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      slot->setOnlyReadsMemory();
576c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      slot->setMetadata(msgSendMDKind, node);
577c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
578c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Load the imp from the slot
579c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
580c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
581c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // The lookup function may have changed the receiver, so make sure we use
582c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // the new one.
583c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      Receiver = Builder.CreateLoad(ReceiverPtr, true);
584c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      return imp;
585c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
586c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
587c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                        llvm::Value *ObjCSuper,
588c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                        llvm::Value *cmd) {
589c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
590c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
591c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
592c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::CallInst *slot = Builder.CreateCall(SlotLookupSuperFn, lookupArgs,
593c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          lookupArgs+2);
594c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      slot->setOnlyReadsMemory();
595c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
596c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      return Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
597c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
5989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
599c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) {
600c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::StructType *SlotStructTy = llvm::StructType::get(VMContext, PtrTy,
601c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          PtrTy, PtrTy, IntTy, IMPTy, NULL);
602c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
603c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
604c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
605c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          SelectorTy, IdTy, NULL);
606c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Slot_t objc_msg_lookup_super(struct objc_super*, SEL);
607c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
608c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              PtrToObjCSuperTy, SelectorTy, NULL);
6099735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      // If we're in ObjC++ mode, then we want to make
6109735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      if (CGM.getLangOptions().CPlusPlus) {
6119735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        const llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
6129735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void *__cxa_begin_catch(void *e)
6139735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, NULL);
6149735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void __cxa_end_catch(void)
6159735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        EnterCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, NULL);
6169735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void _Unwind_Resume_or_Rethrow(void*)
617978d415adab4ef423f5aef430ddfdbefc4cfb000David Chisnall        ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy, PtrTy, NULL);
6189735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      }
619c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
6209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
6219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
6220f984268b05edab2cc555a427c441baa9c252658Chris Lattner} // end anonymous namespace
6230f984268b05edab2cc555a427c441baa9c252658Chris Lattner
62420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
6252a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// Emits a reference to a dummy variable which is emitted with each class.
6262a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// This ensures that a linker error will be generated when trying to link
6272a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// together modules where a referenced class is not defined.
628bb1c8600218b3244340331165fc7cba7bf227655Mike Stumpvoid CGObjCGNU::EmitClassRef(const std::string &className) {
6292a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  std::string symbolRef = "__objc_class_ref_" + className;
6302a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // Don't emit two copies of the same symbol
631bb1c8600218b3244340331165fc7cba7bf227655Mike Stump  if (TheModule.getGlobalVariable(symbolRef))
632bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    return;
6332a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  std::string symbolName = "__objc_class_name_" + className;
6342a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
6352a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  if (!ClassSymbol) {
6361c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
6371c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson        llvm::GlobalValue::ExternalLinkage, 0, symbolName);
6382a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  }
6391c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson  new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
640f35271b8677a5f31498b89587b1da42a06dd46fdChris Lattner    llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
6412a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner}
64220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
6439f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallstatic std::string SymbolNameForMethod(const llvm::StringRef &ClassName,
6449f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    const llvm::StringRef &CategoryName, const Selector MethodName,
6459f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    bool isClassMethod) {
6469f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::string MethodNameColonStripped = MethodName.getAsString();
647d346736dfe5ef584a2d0c9d27d217408ee394b9bDavid Chisnall  std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
648d346736dfe5ef584a2d0c9d27d217408ee394b9bDavid Chisnall      ':', '_');
6499f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return (llvm::Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
6509f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    CategoryName + "_" + MethodNameColonStripped).str();
65187935a8d5c0eeda8ad62815f4fc815ad25c7cd8eDavid Chisnall}
65220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
6539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
6549f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    unsigned protocolClassVersion)
655c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  : CGM(cgm), TheModule(CGM.getModule()), VMContext(cgm.getLLVMContext()),
656c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  ClassPtrAlias(0), MetaClassPtrAlias(0), RuntimeVersion(runtimeABIVersion),
657c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  ProtocolVersion(protocolClassVersion) {
6589f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
659c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall
660c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall  msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
661c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall
6629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CodeGenTypes &Types = CGM.getTypes();
663e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  IntTy = cast<llvm::IntegerType>(
6649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().IntTy));
665e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  LongTy = cast<llvm::IntegerType>(
6669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().LongTy));
6678fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  SizeTy = cast<llvm::IntegerType>(
6689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().getSizeType()));
6698fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  PtrDiffTy = cast<llvm::IntegerType>(
6709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().getPointerDiffType()));
6718fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
673d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Int8Ty = llvm::Type::getInt8Ty(VMContext);
674d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // C string type.  Used in lots of places.
675d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
676d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
6774a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
67820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Zeros[1] = Zeros[0];
679d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
680391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Get the selector Type.
6810d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  QualType selTy = CGM.getContext().getObjCSelType();
6820d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  if (QualType() == selTy) {
6830d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    SelectorTy = PtrToInt8Ty;
6840d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  } else {
6850d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
6860d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  }
687e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
68896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
689391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  PtrTy = PtrToInt8Ty;
6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
691391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Object type
692ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  ASTIdTy = CGM.getContext().getCanonicalType(CGM.getContext().getObjCIdType());
6930d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  if (QualType() == ASTIdTy) {
6940d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    IdTy = PtrToInt8Ty;
6950d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  } else {
6960d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
6970d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  }
698ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
700c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  ObjCSuperTy = llvm::StructType::get(VMContext, IdTy, IdTy, NULL);
701c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
702c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
7039f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
7049f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_exception_throw(id);
7069f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL);
7079735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL);
7089f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // int objc_sync_enter(id);
7099f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, NULL);
7109f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // int objc_sync_exit(id);
7119f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, NULL);
7129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7139f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_enumerationMutation (id)
7149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy,
7159f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      IdTy, NULL);
7169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7179f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
7189f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
7199f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      PtrDiffTy, BoolTy, NULL);
7209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
7219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
7229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      PtrDiffTy, IdTy, BoolTy, BoolTy, NULL);
7239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
7249f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
7259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      PtrDiffTy, BoolTy, BoolTy, NULL);
7269f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
7279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
7289f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      PtrDiffTy, BoolTy, BoolTy, NULL);
7299f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
730391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // IMP type
731391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  std::vector<const llvm::Type*> IMPArgs;
732391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  IMPArgs.push_back(IdTy);
733391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  IMPArgs.push_back(SelectorTy);
734c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
735c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              true));
736ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
7379735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  // Don't bother initialising the GC stuff unless we're compiling in GC mode
738ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
739ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // Get selectors needed in GC mode
740ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    RetainSel = GetNullarySelector("retain", CGM.getContext());
741ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    ReleaseSel = GetNullarySelector("release", CGM.getContext());
742ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
743ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
744ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // Get functions needed in GC mode
745ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
746ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_ivar(id, id, ptrdiff_t);
7479f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy,
7489f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        NULL);
749ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_strongCast (id, id*)
7509f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
7519f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        PtrToIdTy, NULL);
752ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_global(id, id*);
7539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy,
7549f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        NULL);
755ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_weak(id, id*);
7569f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, NULL);
757ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_read_weak(id*);
7589f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, NULL);
759ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // void *objc_memmove_collectable(void*, void *, size_t);
7609f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
7619f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        SizeTy, NULL);
762ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
76320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
764bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
76520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov// This has to perform the lookup every time, since posing and related
76620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov// techniques can modify the name -> class mapping.
76745d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbarllvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
768ddb2a3d55a24a1dbdf9152621642d9a4b4fc2f61Daniel Dunbar                                 const ObjCInterfaceDecl *OID) {
769d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner  llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
77041d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // With the incompatible ABI, this will need to be replaced with a direct
77141d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // reference to the class symbol.  For the compatible nonfragile ABI we are
77241d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // still performing this lookup at run time but emitting the symbol for the
77341d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // class externally so that we can make the switch later.
7742a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  EmitClassRef(OID->getNameAsString());
775ddb2a3d55a24a1dbdf9152621642d9a4b4fc2f61Daniel Dunbar  ClassName = Builder.CreateStructGEP(ClassName, 0);
776ddb2a3d55a24a1dbdf9152621642d9a4b4fc2f61Daniel Dunbar
77726c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian  std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
77820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassLookupFn =
77996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
78026c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian                                                      Params,
78126c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian                                                      true),
78226c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian                              "objc_lookup_class");
78320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return Builder.CreateCall(ClassLookupFn, ClassName);
784391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner}
785391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
78603b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanianllvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel,
7879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    const std::string &TypeEncoding, bool lval) {
7889f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7899f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::SmallVector<TypedSelector, 2> &Types = SelectorTable[Sel];
7909f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::GlobalAlias *SelValue = 0;
7919f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7929f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7939f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  for (llvm::SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
7949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      e = Types.end() ; i!=e ; i++) {
7959f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    if (i->first == TypeEncoding) {
7969f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      SelValue = i->second;
7979f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      break;
7989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    }
7999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  }
8009f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  if (0 == SelValue) {
801c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    SelValue = new llvm::GlobalAlias(SelectorTy,
8029f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                     llvm::GlobalValue::PrivateLinkage,
8039f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                     ".objc_selector_"+Sel.getAsString(), NULL,
8049f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                     &TheModule);
8059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Types.push_back(TypedSelector(TypeEncoding, SelValue));
8069f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  }
8079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
808c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  if (lval) {
809c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Value *tmp = Builder.CreateAlloca(SelValue->getType());
810c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    Builder.CreateStore(SelValue, tmp);
811c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    return tmp;
812c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
813c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  return SelValue;
8149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall}
8159f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
8169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel,
8179f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                    bool lval) {
8189f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetSelector(Builder, Sel, std::string(), lval);
819df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian}
820df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian
8216d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbarllvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
822df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian    *Method) {
823df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  std::string SelTypes;
824df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
8259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetSelector(Builder, Method->getSelector(), SelTypes, false);
8268e67b63530b4f39a48bc12d97376f373a6901279Chris Lattner}
8278e67b63530b4f39a48bc12d97376f373a6901279Chris Lattner
8285a180397870944548aaadeaebf58e415885b9489John McCallllvm::Constant *CGObjCGNU::GetEHType(QualType T) {
8299735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  if (!CGM.getLangOptions().CPlusPlus) {
8309735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      if (T->isObjCIdType()
8319735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall          || T->isObjCQualifiedIdType()) {
8329735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // With the old ABI, there was only one kind of catchall, which broke
8339735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // foreign exceptions.  With the new ABI, we use __objc_id_typeinfo as
8349735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // a pointer indicating object catchalls, and NULL to indicate real
8359735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // catchalls
8369735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        if (CGM.getLangOptions().ObjCNonFragileABI) {
8379735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall          return MakeConstantString("@id");
8389735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        } else {
8399735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall          return 0;
8409735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        }
8419735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      }
8429735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall
8439735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      // All other types should be Objective-C interface pointer types.
8449735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      const ObjCObjectPointerType *OPT =
8459735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        T->getAs<ObjCObjectPointerType>();
8469735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      assert(OPT && "Invalid @catch type.");
8479735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      const ObjCInterfaceDecl *IDecl =
8489735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        OPT->getObjectType()->getInterface();
8499735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      assert(IDecl && "Invalid @catch type.");
8509735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      return MakeConstantString(IDecl->getIdentifier()->getName());
8519735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
85280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // For Objective-C++, we want to provide the ability to catch both C++ and
85380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Objective-C objects in the same function.
85480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
85580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // There's a particular fixed type info for 'id'.
85680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  if (T->isObjCIdType() ||
85780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      T->isObjCQualifiedIdType()) {
85880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    llvm::Constant *IDEHType =
85980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      CGM.getModule().getGlobalVariable("__objc_id_type_info");
86080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    if (!IDEHType)
86180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      IDEHType =
86280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall        new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
86380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall                                 false,
86480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall                                 llvm::GlobalValue::ExternalLinkage,
86580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall                                 0, "__objc_id_type_info");
86680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
86780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  }
86880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
86980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const ObjCObjectPointerType *PT =
87080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    T->getAs<ObjCObjectPointerType>();
87180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  assert(PT && "Invalid @catch type.");
87280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const ObjCInterfaceType *IT = PT->getInterfaceType();
87380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  assert(IT && "Invalid @catch type.");
87480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::string className = IT->getDecl()->getIdentifier()->getName();
87580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
87680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::string typeinfoName = "__objc_eh_typeinfo_" + className;
87780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
87880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Return the existing typeinfo if it exists
87980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
88080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  if (typeinfo) return typeinfo;
88180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
88280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Otherwise create it.
88380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
88480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // vtable for gnustep::libobjc::__objc_class_type_info
88580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // It's quite ugly hard-coding this.  Ideally we'd generate it using the host
88680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // platform's name mangling.
88780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
88880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *Vtable = TheModule.getGlobalVariable(vtableName);
88980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  if (!Vtable) {
89080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
89180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall            llvm::GlobalValue::ExternalLinkage, 0, vtableName);
89280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  }
89380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
89480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  Vtable = llvm::ConstantExpr::getGetElementPtr(Vtable, &Two, 1);
89580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  Vtable = llvm::ConstantExpr::getBitCast(Vtable, PtrToInt8Ty);
89680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
89780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *typeName =
89880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    ExportUniqueString(className, "__objc_eh_typename_");
89980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
90080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::vector<llvm::Constant*> fields;
90180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  fields.push_back(Vtable);
90280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  fields.push_back(typeName);
90380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *TI =
90480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
90580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall              NULL), fields, "__objc_eh_typeinfo_" + className,
90680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall          llvm::GlobalValue::LinkOnceODRLinkage);
90780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
9085a180397870944548aaadeaebf58e415885b9489John McCall}
9095a180397870944548aaadeaebf58e415885b9489John McCall
91020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generate an NSConstantString object.
9110d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnallllvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
91248272a078e54d501cb05edd797c54b8e82ceb520David Chisnall
9132f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer  std::string Str = SL->getString().str();
9140d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall
91548272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  // Look for an existing one
91648272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
91748272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  if (old != ObjCStrings.end())
91848272a078e54d501cb05edd797c54b8e82ceb520David Chisnall    return old->getValue();
91948272a078e54d501cb05edd797c54b8e82ceb520David Chisnall
92020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Ivars;
92120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Ivars.push_back(NULLPtr);
92213fd7e5111032f54b538dd66d035b0ccc1f82467Chris Lattner  Ivars.push_back(MakeConstantString(Str));
9234a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
92420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ObjCStr = MakeGlobal(
92547a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson    llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
92620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Ivars, ".objc_str");
92748272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
92848272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ObjCStrings[Str] = ObjCStr;
92948272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ConstantStrings.push_back(ObjCStr);
93020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return ObjCStr;
93120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
93220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
93320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///Generates a message send where the super is the receiver.  This is a message
93420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///send to self with special delivery semantics indicating which class's method
93520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///should be called.
9369f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallRValue
9379f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
938ef072fd2f3347cfd857d6eb787b245b950771430John McCall                                    ReturnValueSlot Return,
9397f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                                    QualType ResultType,
9407f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                                    Selector Sel,
941f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                                    const ObjCInterfaceDecl *Class,
9427ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian                                    bool isCategoryImpl,
943f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                                    llvm::Value *Receiver,
94419cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar                                    bool IsClassMessage,
945d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                                    const CallArgList &CallArgs,
946d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                                    const ObjCMethodDecl *Method) {
947ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
948ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == RetainSel || Sel == AutoreleaseSel) {
949ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall      return RValue::get(Receiver);
950ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
951ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == ReleaseSel) {
952ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall      return RValue::get(0);
953ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
954ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
955db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
956db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  CGBuilderTy &Builder = CGF.Builder;
957db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  llvm::Value *cmd = GetSelector(Builder, Sel);
958db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
959b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
960b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  CallArgList ActualArgs;
961b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
962b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  ActualArgs.push_back(
963c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      std::make_pair(RValue::get(EnforceType(Builder, Receiver, IdTy)),
9640f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall      ASTIdTy));
965b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  ActualArgs.push_back(std::make_pair(RValue::get(cmd),
966b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian                                      CGF.getContext().getObjCSelType()));
967b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
968b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
969b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  CodeGenTypes &Types = CGM.getTypes();
97004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
971264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                       FunctionType::ExtInfo());
972b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
9735efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  llvm::Value *ReceiverClass = 0;
97448e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner  if (isCategoryImpl) {
97548e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    llvm::Constant *classLookupFunction = 0;
97648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    std::vector<const llvm::Type*> Params;
97748e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    Params.push_back(PtrTy);
97848e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    if (IsClassMessage)  {
97996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
98048e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            IdTy, Params, true), "objc_get_meta_class");
98148e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    } else {
98296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
98348e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            IdTy, Params, true), "objc_get_class");
9845efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    }
985db831948f32217ece8410099f9982ef900c3b3c3David Chisnall    ReceiverClass = Builder.CreateCall(classLookupFunction,
98648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner        MakeConstantString(Class->getNameAsString()));
9875efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  } else {
98848e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // Set up global aliases for the metaclass or class pointer if they do not
98948e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // already exist.  These will are forward-references which will be set to
990bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    // pointers to the class and metaclass structure created for the runtime
991bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    // load function.  To send a message to super, we look up the value of the
99248e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // super_class pointer from either the class or metaclass structure.
99348e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    if (IsClassMessage)  {
99448e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      if (!MetaClassPtrAlias) {
99548e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner        MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
99648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
99748e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            Class->getNameAsString(), NULL, &TheModule);
99848e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      }
99948e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      ReceiverClass = MetaClassPtrAlias;
100048e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    } else {
100148e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      if (!ClassPtrAlias) {
100248e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner        ClassPtrAlias = new llvm::GlobalAlias(IdTy,
100348e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
100448e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            Class->getNameAsString(), NULL, &TheModule);
100548e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      }
100648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      ReceiverClass = ClassPtrAlias;
10075efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    }
100871238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  }
10095efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Cast the pointer to a simplified version of the class structure
1010db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateBitCast(ReceiverClass,
101196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::PointerType::getUnqual(
101247a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson        llvm::StructType::get(VMContext, IdTy, IdTy, NULL)));
10135efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Get the superclass pointer
1014db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1);
10155efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Load the superclass pointer
1016db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateLoad(ReceiverClass);
101720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Construct the structure used to look up the IMP
101847a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ObjCSuperTy = llvm::StructType::get(VMContext,
101947a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson      Receiver->getType(), IdTy, NULL);
1020db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
1021b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1022db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
1023db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
102420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1025c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy);
1026c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  const llvm::FunctionType *impType =
1027c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
1028db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
1029c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // Get the IMP
1030c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd);
1031c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  imp = EnforceType(Builder, imp, llvm::PointerType::getUnqual(impType));
103220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1033dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall  llvm::Value *impMD[] = {
1034dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::MDString::get(VMContext, Sel.getAsString()),
1035dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
1036dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsClassMessage)
1037dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall   };
1038dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall  llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD, 3);
1039dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall
10404b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  llvm::Instruction *call;
1041ef072fd2f3347cfd857d6eb787b245b950771430John McCall  RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs,
10424b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall      0, &call);
10434b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  call->setMetadata(msgSendMDKind, node);
10444b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  return msgRet;
104520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
104620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Generate code for a message send expression.
10489f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallRValue
10499f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
1050ef072fd2f3347cfd857d6eb787b245b950771430John McCall                               ReturnValueSlot Return,
10517f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                               QualType ResultType,
10527f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                               Selector Sel,
1053f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                               llvm::Value *Receiver,
1054df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                               const CallArgList &CallArgs,
1055c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                               const ObjCInterfaceDecl *Class,
1056df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                               const ObjCMethodDecl *Method) {
1057664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // Strip out message sends to retain / release in GC mode
1058ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
1059ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == RetainSel || Sel == AutoreleaseSel) {
1060ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall      return RValue::get(Receiver);
1061ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
1062ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == ReleaseSel) {
1063ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall      return RValue::get(0);
1064ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
1065ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
1066664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1067d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  CGBuilderTy &Builder = CGF.Builder;
1068664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1069664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // If the return type is something that goes in an integer register, the
1070664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // runtime will handle 0 returns.  For other cases, we fill in the 0 value
1071664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // ourselves.
1072664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  //
1073664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // The language spec says the result of this kind of message send is
1074664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // undefined, but lots of people seem to have forgotten to read that
1075664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // paragraph and insist on sending messages to nil that have structure
1076664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // returns.  With GCC, this generates a random return value (whatever happens
1077664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // to be on the stack / in those registers at the time) on most platforms,
1078c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // and generates an illegal instruction trap on SPARC.  With LLVM it corrupts
1079c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // the stack.
1080c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  bool isPointerSizedReturn = (ResultType->isAnyPointerType() ||
1081c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType());
1082664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1083664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  llvm::BasicBlock *startBB = 0;
1084664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  llvm::BasicBlock *messageBB = 0;
1085a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall  llvm::BasicBlock *continueBB = 0;
1086664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1087664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  if (!isPointerSizedReturn) {
1088664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    startBB = Builder.GetInsertBlock();
1089664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    messageBB = CGF.createBasicBlock("msgSend");
1090a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    continueBB = CGF.createBasicBlock("continue");
1091664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1092664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
1093664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall            llvm::Constant::getNullValue(Receiver->getType()));
1094a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    Builder.CreateCondBr(isNil, continueBB, messageBB);
1095664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    CGF.EmitBlock(messageBB);
1096664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  }
1097664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
10980f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
1099df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  llvm::Value *cmd;
1100df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  if (Method)
1101d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    cmd = GetSelector(Builder, Method);
1102df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  else
1103d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    cmd = GetSelector(Builder, Sel);
1104c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  cmd = EnforceType(Builder, cmd, SelectorTy);
1105c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  Receiver = EnforceType(Builder, Receiver, IdTy);
1106c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
1107c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Value *impMD[] = {
1108c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        llvm::MDString::get(VMContext, Sel.getAsString()),
1109c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        llvm::MDString::get(VMContext, Class ? Class->getNameAsString() :""),
1110c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), Class!=0)
1111c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall   };
1112c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD, 3);
1113b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1114c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // Get the IMP to call
1115c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Value *imp = LookupIMP(CGF, Receiver, cmd, node);
1116c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
1117c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  CallArgList ActualArgs;
1118b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  ActualArgs.push_back(
1119d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    std::make_pair(RValue::get(Receiver), ASTIdTy));
1120b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  ActualArgs.push_back(std::make_pair(RValue::get(cmd),
1121b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian                                      CGF.getContext().getObjCSelType()));
1122b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
1123b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1124b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  CodeGenTypes &Types = CGM.getTypes();
112504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
1126264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                       FunctionType::ExtInfo());
11276793966aa221764f579cc8436bd641e1ec339c6dDaniel Dunbar  const llvm::FunctionType *impType =
11286793966aa221764f579cc8436bd641e1ec339c6dDaniel Dunbar    Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
1129c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  imp = EnforceType(Builder, imp, llvm::PointerType::getUnqual(impType));
113063e742bed90aab2e8e778e48fbbd4bce766b4373David Chisnall
113163e742bed90aab2e8e778e48fbbd4bce766b4373David Chisnall
113234e65770ad6142b033d6cec4f476ed20fb248bf3Fariborz Jahanian  // For sender-aware dispatch, we pass the sender as the third argument to a
113334e65770ad6142b033d6cec4f476ed20fb248bf3Fariborz Jahanian  // lookup function.  When sending messages from C code, the sender is nil.
1134d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
11354b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  llvm::Instruction *call;
1136ef072fd2f3347cfd857d6eb787b245b950771430John McCall  RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs,
11374b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall      0, &call);
11384b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  call->setMetadata(msgSendMDKind, node);
1139664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1140a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall
1141664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  if (!isPointerSizedReturn) {
1142a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    messageBB = CGF.Builder.GetInsertBlock();
1143a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    CGF.Builder.CreateBr(continueBB);
1144a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    CGF.EmitBlock(continueBB);
1145664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    if (msgRet.isScalar()) {
1146664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      llvm::Value *v = msgRet.getScalarVal();
1147bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
1148664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v, messageBB);
1149664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
1150664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::get(phi);
1151664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    } else if (msgRet.isAggregate()) {
1152664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      llvm::Value *v = msgRet.getAggregateAddr();
1153bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
1154664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      const llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType());
1155866163bf6c66211a9b4d5ba160ac02de04ce4be6David Chisnall      llvm::AllocaInst *NullVal =
1156866163bf6c66211a9b4d5ba160ac02de04ce4be6David Chisnall          CGF.CreateTempAlloca(RetTy->getElementType(), "null");
1157664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      CGF.InitTempAlloca(NullVal,
1158664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          llvm::Constant::getNullValue(RetTy->getElementType()));
1159664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v, messageBB);
1160664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(NullVal, startBB);
1161664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::getAggregate(phi);
1162664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    } else /* isComplex() */ {
1163664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
1164bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
1165664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v.first, messageBB);
1166664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
1167664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          startBB);
1168bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
1169664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi2->addIncoming(v.second, messageBB);
1170664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
1171664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          startBB);
1172664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::getComplex(phi, phi2);
1173664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    }
1174664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  }
1175664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  return msgRet;
117620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
117720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Generates a MethodList.  Used in construction of a objc_class and
117920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// objc_category structures.
11809f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Constant *CGObjCGNU::GenerateMethodList(const llvm::StringRef &ClassName,
11819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                              const llvm::StringRef &CategoryName,
11821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const llvm::SmallVectorImpl<Selector> &MethodSels,
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
118420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    bool isClassMethodList) {
11850f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  if (MethodSels.empty())
11860f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    return NULLPtr;
11871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
118847a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ObjCMethodTy = llvm::StructType::get(VMContext,
118920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Really a selector, but the runtime creates it us.
119020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Method types
1191c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    IMPTy, //Method pointer
119220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    NULL);
119320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Methods;
119420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
119520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
119620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
11979f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *Method =
119820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
11999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                                MethodSels[i],
12009f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                                isClassMethodList));
12019f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    assert(Method && "Can't generate metadata for method that doesn't exist");
12029f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
12039f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(C);
12049f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(MethodTypes[i]);
12059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Method = llvm::ConstantExpr::getBitCast(Method,
1206c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        IMPTy);
12079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(Method);
12089f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
120920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
121020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
121120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of method structures
121296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
12131e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian                                                            Methods.size());
12147db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
1215fba676396703e87a5034e5f308aa30a633468f66Chris Lattner                                                         Methods);
121620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
121720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Structure containing list pointer, array and array count
121820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
12198c8f69ec1477424e11faef6b235505584d61e522Owen Anderson  llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext);
122096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
122147a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ObjCMethodListTy = llvm::StructType::get(VMContext,
12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NextPtrTy,
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      IntTy,
122420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ObjCMethodArrayTy,
122520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      NULL);
122620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Refine next pointer type to concrete type
122720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::cast<llvm::OpaqueType>(
122820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
122920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
123020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
123120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.clear();
123203e205031b08669f05c41eed5b896fc94c4a12bbOwen Anderson  Methods.push_back(llvm::ConstantPointerNull::get(
123396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson        llvm::PointerType::getUnqual(ObjCMethodListTy)));
12340032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
123520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        MethodTypes.size()));
123620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.push_back(MethodArray);
12371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
123820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
123920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
124020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
124120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
124220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generates an IvarList.  Used in construction of a objc_class.
124320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateIvarList(
124420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    const llvm::SmallVectorImpl<llvm::Constant *>  &IvarNames,
124520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    const llvm::SmallVectorImpl<llvm::Constant *>  &IvarTypes,
124620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    const llvm::SmallVectorImpl<llvm::Constant *>  &IvarOffsets) {
12471804463ee790ac3cc73f72597ccb38b983f7d263David Chisnall  if (IvarNames.size() == 0)
12481804463ee790ac3cc73f72597ccb38b983f7d263David Chisnall    return NULLPtr;
12491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
125047a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ObjCIvarTy = llvm::StructType::get(VMContext,
125120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
125220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
125320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    IntTy,
125420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    NULL);
125520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Ivars;
125620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
125720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
125820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
12598a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(IvarNames[i]);
12608a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(IvarTypes[i]);
126120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.push_back(IvarOffsets[i]);
126208e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson    Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
126320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
126420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
126520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of method structures
126696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
126720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IvarNames.size());
126820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
12691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
127020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
12714a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
12727db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
127320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Structure containing array and array count
127447a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ObjCIvarListTy = llvm::StructType::get(VMContext, IntTy,
127520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    ObjCIvarArrayTy,
127620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    NULL);
127720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
127820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
127920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
12800f984268b05edab2cc555a427c441baa9c252658Chris Lattner}
12810f984268b05edab2cc555a427c441baa9c252658Chris Lattner
128220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generate a class structure
128320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateClassStructure(
128420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *MetaClass,
128520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *SuperClass,
128620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    unsigned info,
1287d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner    const char *Name,
128820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Version,
128920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *InstanceSize,
129020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *IVars,
129120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Methods,
1292d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *Protocols,
1293d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *IvarOffsets,
12948c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall    llvm::Constant *Properties,
12958c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall    bool isMeta) {
129620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Set up the class structure
129720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Note:  Several of these are char*s when they should be ids.  This is
129820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // because the runtime performs this translation on load.
1299d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  //
1300d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Fields marked New ABI are part of the GNUstep runtime.  We emit them
1301d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // anyway; the classes will still work with the GNU runtime, they will just
1302d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // be ignored.
130347a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ClassTy = llvm::StructType::get(VMContext,
130420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,        // class_pointer
130520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,        // super_class
130620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,        // name
130720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // version
130820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // info
130920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // instance_size
131020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IVars->getType(),   // ivars
131120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Methods->getType(), // methods
13121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // These are all filled in by the runtime, so we pretend
131320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // dtable
131420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // subclass_list
131520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // sibling_class
131620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // protocols
131720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // gc_object_type
1318d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      // New ABI:
1319d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      LongTy,                 // abi_version
1320d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      IvarOffsets->getType(), // ivar_offsets
1321d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Properties->getType(),  // properties
132220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      NULL);
13234a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
132420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Fill in the structure
132520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
13263c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
132720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(SuperClass);
1328d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner  Elements.push_back(MakeConstantString(Name, ".class_name"));
132920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(Zero);
13304a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(LongTy, info));
133105f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall  if (isMeta) {
133205f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall    llvm::TargetData td(&TheModule);
133305f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall    Elements.push_back(llvm::ConstantInt::get(LongTy,
133405f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall                     td.getTypeSizeInBits(ClassTy)/8));
133505f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall  } else
133605f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall    Elements.push_back(InstanceSize);
133720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(IVars);
133820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(Methods);
1339d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1340d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1341d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
13423c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
1343d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1344d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(Zero);
1345d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(IvarOffsets);
1346d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(Properties);
134720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
134841d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // This is now an externally visible symbol, so that we can speed up class
134941d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // messages in the next ABI.
13508c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall  return MakeGlobal(ClassTy, Elements, (isMeta ? "_OBJC_METACLASS_":
13518c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall      "_OBJC_CLASS_") + std::string(Name), llvm::GlobalValue::ExternalLinkage);
135220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
135320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
135420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
135520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    const llvm::SmallVectorImpl<llvm::Constant *>  &MethodNames,
135620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    const llvm::SmallVectorImpl<llvm::Constant *>  &MethodTypes) {
13571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
135847a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(VMContext,
135920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
136020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
136120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    NULL);
136220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Methods;
136320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
136420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
136520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
13661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Elements.push_back(MethodNames[i]);
13678a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(MethodTypes[i]);
136808e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson    Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
136920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
137096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
137120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      MethodNames.size());
13727db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
1373bb1c8600218b3244340331165fc7cba7bf227655Mike Stump                                                   Methods);
137447a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(VMContext,
137520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IntTy, ObjCMethodArrayTy, NULL);
137620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.clear();
13774a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
137820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.push_back(Array);
137920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
138020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
1381bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
138220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov// Create the protocol list structure used in classes, categories and so on
138320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateProtocolList(
138420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    const llvm::SmallVectorImpl<std::string> &Protocols) {
138596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
138620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Protocols.size());
138747a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
138820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy, //Should be a recurisve pointer, but it's always NULL here.
13898fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall      SizeTy,
139020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ProtocolArrayTy,
139120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      NULL);
13921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
139320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
139420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      iter != endIter ; iter++) {
1395ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    llvm::Constant *protocol = 0;
1396ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    llvm::StringMap<llvm::Constant*>::iterator value =
1397ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall      ExistingProtocols.find(*iter);
1398ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    if (value == ExistingProtocols.end()) {
1399f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      protocol = GenerateEmptyProtocol(*iter);
1400ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    } else {
1401ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall      protocol = value->getValue();
1402ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    }
14033c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
1404a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson                                                           PtrToInt8Ty);
140520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.push_back(Ptr);
140620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
14077db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
140820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Elements);
140920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
141020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
14114a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
141220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ProtocolArray);
141320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
141420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
141520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
14161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
1417af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar                                            const ObjCProtocolDecl *PD) {
1418f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
14191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *T =
1420f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian    CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
142196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
1422f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian}
1423f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian
1424f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanianllvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
1425f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  const std::string &ProtocolName) {
1426f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::SmallVector<std::string, 0> EmptyStringVector;
1427f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
1428f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian
1429f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
1430d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *MethodList =
1431f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian    GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
1432f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // Protocols are objects containing lists of the methods implemented and
1433f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // protocols adopted.
143447a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
1435f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      PtrToInt8Ty,
1436f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      ProtocolList->getType(),
1437d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1438d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1439d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1440d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1441f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      NULL);
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
1443f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // The isa pointer must be set to a magic number so the runtime knows it's
1444f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // the correct layout.
14453c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getIntToPtr(
14469f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
14479f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall          ProtocolVersion), IdTy));
1448f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1449f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  Elements.push_back(ProtocolList);
1450d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1451d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1452d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1453d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1454f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
145520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
145620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1457af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbarvoid CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
1458af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  ASTContext &Context = CGM.getContext();
14598ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ProtocolName = PD->getNameAsString();
1460af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  llvm::SmallVector<std::string, 16> Protocols;
1461af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
1462af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar       E = PD->protocol_end(); PI != E; ++PI)
1463d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner    Protocols.push_back((*PI)->getNameAsString());
1464af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
1465af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
1466d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
1467d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
146817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
146917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->instmeth_end(); iter != E; iter++) {
1470af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    std::string TypeStr;
1471af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
1472d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1473d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodNames.push_back(
1474d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          MakeConstantString((*iter)->getSelector().getAsString()));
1475d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1476d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1477d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodNames.push_back(
1478d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          MakeConstantString((*iter)->getSelector().getAsString()));
1479d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1480d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1481af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  }
1482af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  // Collect information about class methods:
1483af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
1484af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
1485d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
1486d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (ObjCProtocolDecl::classmeth_iterator
148817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
148917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       iter != endIter ; iter++) {
1490af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    std::string TypeStr;
1491af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
1492d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1493d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ClassMethodNames.push_back(
1494d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          MakeConstantString((*iter)->getSelector().getAsString()));
1495d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ClassMethodTypes.push_back(MakeConstantString(TypeStr));
1496d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1497d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodNames.push_back(
1498d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          MakeConstantString((*iter)->getSelector().getAsString()));
1499d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
1500d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1501af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  }
150220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
150320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
150420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *InstanceMethodList =
150520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
150620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassMethodList =
150720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
1508d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalInstanceMethodList =
1509d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateProtocolMethodList(OptionalInstanceMethodNames,
1510d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            OptionalInstanceMethodTypes);
1511d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalClassMethodList =
1512d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateProtocolMethodList(OptionalClassMethodNames,
1513d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            OptionalClassMethodTypes);
1514d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1515d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Property metadata: name, attributes, isSynthesized, setter name, setter
1516d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // types, getter name, getter types.
1517d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // The isSynthesized value is always set to 0 in a protocol.  It exists to
1518d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // simplify the runtime library by allowing it to use the same data
1519d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // structures for protocol metadata everywhere.
1520d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
1521d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1522d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          PtrToInt8Ty, NULL);
1523d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Properties;
1524d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> OptionalProperties;
1525d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1526d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all of the property methods need adding to the method list and to the
1527d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // property metadata list.
1528d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  for (ObjCContainerDecl::prop_iterator
1529d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian         iter = PD->prop_begin(), endIter = PD->prop_end();
1530d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       iter != endIter ; iter++) {
1531d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    std::vector<llvm::Constant*> Fields;
1532d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    ObjCPropertyDecl *property = (*iter);
1533d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1534d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(MakeConstantString(property->getNameAsString()));
1535d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1536d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian                property->getPropertyAttributes()));
1537d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
1538d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1539d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1540d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1541d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1542d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodTypes.push_back(TypeEncoding);
1543d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1544d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1545d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1546d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1547d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1548d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1549d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1550d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1551d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1552d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1553d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodTypes.push_back(TypeEncoding);
1554d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1555d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1556d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1557d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1558d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1559d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1560d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1561d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1562d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1563d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1564d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1565d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
1566d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1567d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1568d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* PropertyListInitFields[] =
1569d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1570d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1571d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyListInit =
15720d36dd28bb40b08a93d7f64196794882eee08ee8Nick Lewycky      llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
1573d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1574d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1575d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyListInit, ".objc_property_list");
1576d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1577d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyArray =
1578d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1579d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          OptionalProperties.size()) , OptionalProperties);
1580d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* OptionalPropertyListInitFields[] = {
1581d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1582d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalPropertyArray };
1583d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1584d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyListInit =
15850d36dd28bb40b08a93d7f64196794882eee08ee8Nick Lewycky      llvm::ConstantStruct::get(VMContext, OptionalPropertyListInitFields, 3, false);
1586d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1587d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          OptionalPropertyListInit->getType(), false,
1588d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1589d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ".objc_property_list");
1590d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
159120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Protocols are objects containing lists of the methods implemented and
159220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // protocols adopted.
159347a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
159420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,
159520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ProtocolList->getType(),
159620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      InstanceMethodList->getType(),
159720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ClassMethodList->getType(),
1598d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodList->getType(),
1599d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodList->getType(),
1600d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyList->getType(),
1601d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalPropertyList->getType(),
160220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      NULL);
16031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
160420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // The isa pointer must be set to a magic number so the runtime knows it's
160520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // the correct layout.
16063c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getIntToPtr(
16079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
16089f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall          ProtocolVersion), IdTy));
160920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
161020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ProtocolList);
161120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(InstanceMethodList);
161220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ClassMethodList);
1613d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalInstanceMethodList);
1614d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalClassMethodList);
1615d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(PropertyList);
1616d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalPropertyList);
16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ExistingProtocols[ProtocolName] =
16183c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
161920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          ".objc_protocol"), IdTy);
162020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
1621d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanianvoid CGObjCGNU::GenerateProtocolHolderCategory(void) {
1622d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Collect information about instance methods
1623d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::SmallVector<Selector, 1> MethodSels;
1624d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::SmallVector<llvm::Constant*, 1> MethodTypes;
1625d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1626d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Elements;
1627d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1628d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  const std::string CategoryName = "AnotherHack";
1629d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MakeConstantString(CategoryName));
1630d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MakeConstantString(ClassName));
1631d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Instance method list
1632d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1633d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1634d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Class method list
1635d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1636d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1637d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Protocol list
1638d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1639d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ExistingProtocols.size());
1640d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
1641d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PtrTy, //Should be a recurisve pointer, but it's always NULL here.
16428fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall      SizeTy,
1643d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ProtocolArrayTy,
1644d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      NULL);
1645d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> ProtocolElements;
1646d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  for (llvm::StringMapIterator<llvm::Constant*> iter =
1647d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1648d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       iter != endIter ; iter++) {
1649d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1650d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            PtrTy);
1651d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    ProtocolElements.push_back(Ptr);
1652d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
1653d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1654d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ProtocolElements);
1655d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.clear();
1656d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(NULLPtr);
1657d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1658d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian              ExistingProtocols.size()));
1659d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(ProtocolArray);
1660d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1661d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian                  ProtocolElements, ".objc_protocol_list"), PtrTy));
1662d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Categories.push_back(llvm::ConstantExpr::getBitCast(
1663d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian        MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
1664d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
1665d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian}
166620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
16677ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbarvoid CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
16688ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ClassName = OCD->getClassInterface()->getNameAsString();
16698ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string CategoryName = OCD->getNameAsString();
16707ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance methods
16717ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<Selector, 16> InstanceMethodSels;
16727ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
1673653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor  for (ObjCCategoryImplDecl::instmeth_iterator
167417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
1675653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor       iter != endIter ; iter++) {
16767ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    InstanceMethodSels.push_back((*iter)->getSelector());
16777ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
16787ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
16798a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
16807ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
16817ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
16827ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about class methods
16837ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<Selector, 16> ClassMethodSels;
16847ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (ObjCCategoryImplDecl::classmeth_iterator
168617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
1687653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor       iter != endIter ; iter++) {
16887ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    ClassMethodSels.push_back((*iter)->getSelector());
16897ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
16907ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
16918a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    ClassMethodTypes.push_back(MakeConstantString(TypeStr));
16927ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
16937ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
16947ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect the names of referenced protocols
16957ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<std::string, 16> Protocols;
1696ad9e06d82ae627e858e5bfe4a7f1f9337b5b0a15David Chisnall  const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
1697ad9e06d82ae627e858e5bfe4a7f1f9337b5b0a15David Chisnall  const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
16987ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
16997ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar       E = Protos.end(); I != E; ++I)
1700d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner    Protocols.push_back((*I)->getNameAsString());
17017ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
170220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
170320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(CategoryName));
170420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(ClassName));
17051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Instance method list
17063c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1707a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner          ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
170820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          false), PtrTy));
170920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Class method list
17103c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1711a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner          ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
171220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        PtrTy));
171320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Protocol list
17143c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(
171520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        GenerateProtocolList(Protocols), PtrTy));
17163c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Categories.push_back(llvm::ConstantExpr::getBitCast(
17171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
171847a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson            PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
171920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
17207ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
1721d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanianllvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
1722d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian        llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
1723d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian        llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
1724d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ASTContext &Context = CGM.getContext();
1725d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  //
1726d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Property metadata: name, attributes, isSynthesized, setter name, setter
1727d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // types, getter name, getter types.
1728d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
1729d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1730d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          PtrToInt8Ty, NULL);
1731d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Properties;
1732d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1733d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1734d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all of the property methods need adding to the method list and to the
1735d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // property metadata list.
1736d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  for (ObjCImplDecl::propimpl_iterator
1737d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian         iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
1738d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       iter != endIter ; iter++) {
1739d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    std::vector<llvm::Constant*> Fields;
1740d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
174142ba04a072c69d84a6660e4498215086662910acDavid Chisnall    ObjCPropertyImplDecl *propertyImpl = *iter;
174242ba04a072c69d84a6660e4498215086662910acDavid Chisnall    bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
174342ba04a072c69d84a6660e4498215086662910acDavid Chisnall        ObjCPropertyImplDecl::Synthesize);
1744d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1745d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(MakeConstantString(property->getNameAsString()));
1746d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1747d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian                property->getPropertyAttributes()));
174842ba04a072c69d84a6660e4498215086662910acDavid Chisnall    Fields.push_back(llvm::ConstantInt::get(Int8Ty, isSynthesized));
1749d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1750d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1751d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1752d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
175342ba04a072c69d84a6660e4498215086662910acDavid Chisnall      if (isSynthesized) {
175442ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodTypes.push_back(TypeEncoding);
175542ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodSels.push_back(getter->getSelector());
175642ba04a072c69d84a6660e4498215086662910acDavid Chisnall      }
1757d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1758d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1759d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1760d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1761d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1762d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1763d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1764d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1765d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1766d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
176742ba04a072c69d84a6660e4498215086662910acDavid Chisnall      if (isSynthesized) {
176842ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodTypes.push_back(TypeEncoding);
176942ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodSels.push_back(setter->getSelector());
177042ba04a072c69d84a6660e4498215086662910acDavid Chisnall      }
1771d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1772d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1773d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1774d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1775d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1776d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1777d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1778d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
1779d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::ArrayType *PropertyArrayTy =
1780d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
1781d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
1782d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          Properties);
1783d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* PropertyListInitFields[] =
1784d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1785d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1786d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyListInit =
17870d36dd28bb40b08a93d7f64196794882eee08ee8Nick Lewycky      llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
1788d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
1789d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          llvm::GlobalValue::InternalLinkage, PropertyListInit,
1790d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ".objc_property_list");
1791d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian}
1792d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
17937ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbarvoid CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
17947ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  ASTContext &Context = CGM.getContext();
17957ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
17967ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Get the superclass name.
17971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ObjCInterfaceDecl * SuperClassDecl =
17987ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    OID->getClassInterface()->getSuperClass();
17998ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string SuperClassName;
18002a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  if (SuperClassDecl) {
18018ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner    SuperClassName = SuperClassDecl->getNameAsString();
18022a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner    EmitClassRef(SuperClassName);
18032a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  }
18047ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
18057ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Get the class name
180609dc6660487fc2f4a4bb2032e30123d3e0da7230Chris Lattner  ObjCInterfaceDecl *ClassDecl =
180709dc6660487fc2f4a4bb2032e30123d3e0da7230Chris Lattner    const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
18088ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ClassName = ClassDecl->getNameAsString();
18092a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // Emit the symbol that is used to generate linker errors if this class is
18102a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // referenced in other modules but not declared.
1811c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  std::string classSymbolName = "__objc_class_name_" + ClassName;
18121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::GlobalVariable *symbol =
1813c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian      TheModule.getGlobalVariable(classSymbolName)) {
18144a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson    symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
1815c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  } else {
18161c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    new llvm::GlobalVariable(TheModule, LongTy, false,
18174a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson    llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
18181c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    classSymbolName);
1819c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  }
18201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18212bebbf0acee55404de4b8846713b64429e744e8fDaniel Dunbar  // Get the size of instances.
18225f022d82696c84e4d127c558871d68ac6273274eKen Dyck  int instanceSize =
18235f022d82696c84e4d127c558871d68ac6273274eKen Dyck    Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
18247ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
18257ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance variables.
18267ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> IvarNames;
18277ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
18287ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
18291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1830d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> IvarOffsetValues;
1831d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
18321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  int superInstanceSize = !SuperClassDecl ? 0 :
18335f022d82696c84e4d127c558871d68ac6273274eKen Dyck    Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
18349cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // For non-fragile ivars, set the instance size to 0 - {the size of just this
18359cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // class}.  The runtime will then set this to the correct value on load.
18369cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
18379cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    instanceSize = 0 - (instanceSize - superInstanceSize);
18389cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
18397f63cb098f2461643f71ea590c1a477603f27f63David Chisnall
18407f63cb098f2461643f71ea590c1a477603f27f63David Chisnall  // Collect declared and synthesized ivars.
18417f63cb098f2461643f71ea590c1a477603f27f63David Chisnall  llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
18427f63cb098f2461643f71ea590c1a477603f27f63David Chisnall  CGM.getContext().ShallowCollectObjCIvars(ClassDecl, OIvars);
18437f63cb098f2461643f71ea590c1a477603f27f63David Chisnall
18447f63cb098f2461643f71ea590c1a477603f27f63David Chisnall  for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
18457f63cb098f2461643f71ea590c1a477603f27f63David Chisnall      ObjCIvarDecl *IVD = OIvars[i];
18467ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Store the name
18477f63cb098f2461643f71ea590c1a477603f27f63David Chisnall      IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
18487ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Get the type encoding for this ivar
18497ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      std::string TypeStr;
18507f63cb098f2461643f71ea590c1a477603f27f63David Chisnall      Context.getObjCEncodingForType(IVD->getType(), TypeStr);
18518a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarTypes.push_back(MakeConstantString(TypeStr));
18527ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Get the offset
1853d901da531433254210a08e8b1f9e1ad049b340aaDavid Chisnall      uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
1854aecbf24a6bad049437f8fec97e557d414003a3dbDavid Chisnall      uint64_t Offset = BaseOffset;
18559cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
1856d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian        Offset = BaseOffset - superInstanceSize;
18579cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      }
18587ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      IvarOffsets.push_back(
18590032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson          llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset));
1860d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      IvarOffsetValues.push_back(new llvm::GlobalVariable(TheModule, IntTy,
1861d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          false, llvm::GlobalValue::ExternalLinkage,
1862e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall          llvm::ConstantInt::get(IntTy, Offset),
1863d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          "__objc_ivar_offset_value_" + ClassName +"." +
18647f63cb098f2461643f71ea590c1a477603f27f63David Chisnall          IVD->getNameAsString()));
18657ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
18669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::GlobalVariable *IvarOffsetArray =
18679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    MakeGlobalArray(PtrToIntTy, IvarOffsetValues, ".ivar.offsets");
18689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
18697ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
18707ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance methods
18717ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<Selector, 16> InstanceMethodSels;
18727ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
18731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (ObjCImplementationDecl::instmeth_iterator
187417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
1875653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor       iter != endIter ; iter++) {
18767ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    InstanceMethodSels.push_back((*iter)->getSelector());
18777ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
18787ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
18798a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
18807ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
1881d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1882d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
1883d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          InstanceMethodTypes);
1884d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
18857ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
18867ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about class methods
18877ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<Selector, 16> ClassMethodSels;
18887ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
1889653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor  for (ObjCImplementationDecl::classmeth_iterator
189017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
1891653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor       iter != endIter ; iter++) {
18927ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    ClassMethodSels.push_back((*iter)->getSelector());
18937ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
18947ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
18958a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    ClassMethodTypes.push_back(MakeConstantString(TypeStr));
18967ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
18977ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect the names of referenced protocols
18987ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  llvm::SmallVector<std::string, 16> Protocols;
18997ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
19007ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
19017ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar       E = Protos.end(); I != E; ++I)
1902d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner    Protocols.push_back((*I)->getNameAsString());
19037ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
19047ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
19057ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
190620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Get the superclass pointer.
190720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *SuperClass;
19088ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  if (!SuperClassName.empty()) {
190920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
191020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  } else {
191103e205031b08669f05c41eed5b896fc94c4a12bbOwen Anderson    SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
191220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
191320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Empty vector used to construct empty method lists
191420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::SmallVector<llvm::Constant*, 1>  empty;
191520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate the method and instance variable lists
191620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
1917a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner      InstanceMethodSels, InstanceMethodTypes, false);
191820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
1919a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner      ClassMethodSels, ClassMethodTypes, true);
192020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
192120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IvarOffsets);
19221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
19238a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // we emit a symbol containing the offset for each ivar in the class.  This
19248a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // allows code compiled for the non-Fragile ABI to inherit from code compiled
19258a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // for the legacy ABI, without causing problems.  The converse is also
19268a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // possible, but causes all ivar accesses to be fragile.
1927e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall
19288a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // Offset pointer for getting at the correct field in the ivar list when
19298a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // setting up the alias.  These are: The base address for the global, the
19308a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // ivar array (second field), the ivar in this list (set for each ivar), and
19318a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // the offset (third field in ivar structure)
19328a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext);
19338a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
19341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      llvm::ConstantInt::get(IndexTy, 1), 0,
19358a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::ConstantInt::get(IndexTy, 2) };
19368a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall
1937e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall
1938e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall  for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
1939e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall      ObjCIvarDecl *IVD = OIvars[i];
19408a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
1941e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall          + IVD->getNameAsString();
1942e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall      offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i);
19438a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      // Get the correct ivar field
19448a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
19458a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall              IvarList, offsetPointerIndexes, 4);
1946e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall      // Get the existing variable, if one exists.
19478a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
19488a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      if (offset) {
19498a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          offset->setInitializer(offsetValue);
19508a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          // If this is the real definition, change its linkage type so that
19518a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          // different modules will use this one, rather than their private
19528a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          // copy.
19538a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
19548a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      } else {
19558a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          // Add a new alias if there isn't one already.
19568a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
19578a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall                  false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
19588a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      }
19598a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  }
196020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  //Generate metaclass for class methods
196120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
19621804463ee790ac3cc73f72597ccb38b983f7d263David Chisnall      NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList(
19638c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall        empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr, true);
19645efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
196520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate the class structure
19668ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  llvm::Constant *ClassStruct =
1967d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
19688ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner                           ClassName.c_str(), 0,
19694a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
1970d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
1971d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Properties);
19725efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
19735efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Resolve the class aliases, if they exist.
19745efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  if (ClassPtrAlias) {
19750b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    ClassPtrAlias->replaceAllUsesWith(
19763c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson        llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
19770b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    ClassPtrAlias->eraseFromParent();
19785efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    ClassPtrAlias = 0;
19795efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  }
19805efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  if (MetaClassPtrAlias) {
19810b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    MetaClassPtrAlias->replaceAllUsesWith(
19823c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson        llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
19830b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    MetaClassPtrAlias->eraseFromParent();
19845efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    MetaClassPtrAlias = 0;
19855efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  }
19865efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
198720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Add class structure to list to be added to the symtab later
19883c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
198920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(ClassStruct);
199020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
199120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1992c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian
19931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function *CGObjCGNU::ModuleInitFunction() {
199420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Only emit an ObjC load function if no Objective-C stuff has been called
199520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
19969f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      ExistingProtocols.empty() && SelectorTable.empty())
199720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    return NULL;
19981b8956e1ce4c5770982d6e59edb16979b500677aEli Friedman
1999d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all referenced protocols to a category.
2000d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  GenerateProtocolHolderCategory();
2001d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
2002e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
2003e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner          SelectorTy->getElementType());
2004e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  const llvm::Type *SelStructPtrTy = SelectorTy;
2005e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  bool isSelOpaque = false;
2006e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  if (SelStructTy == 0) {
200747a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson    SelStructTy = llvm::StructType::get(VMContext, PtrToInt8Ty,
200847a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson                                        PtrToInt8Ty, NULL);
200996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
2010e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    isSelOpaque = true;
2011e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  }
2012e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
20131b8956e1ce4c5770982d6e59edb16979b500677aEli Friedman  // Name the ObjC types to make the IR a bit easier to read
2014e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  TheModule.addTypeName(".objc_selector", SelStructPtrTy);
20151b8956e1ce4c5770982d6e59edb16979b500677aEli Friedman  TheModule.addTypeName(".objc_id", IdTy);
20161b8956e1ce4c5770982d6e59edb16979b500677aEli Friedman  TheModule.addTypeName(".objc_imp", IMPTy);
20171b8956e1ce4c5770982d6e59edb16979b500677aEli Friedman
201820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
201971238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  llvm::Constant *Statics = NULLPtr;
202020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate statics list:
202171238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  if (ConstantStrings.size()) {
202296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
202371238f67c5df13eef19450d66b7a7ab28377192aChris Lattner        ConstantStrings.size() + 1);
202471238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    ConstantStrings.push_back(NULLPtr);
20258a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall
20261b0969590e712d7d52fc9c0d43d3ab85c36d07a6Daniel Dunbar    llvm::StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass;
20279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20281b0969590e712d7d52fc9c0d43d3ab85c36d07a6Daniel Dunbar    if (StringClass.empty()) StringClass = "NXConstantString";
20299f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20308a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(MakeConstantString(StringClass,
20318a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall                ".objc_static_class_name"));
20327db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson    Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
203371238f67c5df13eef19450d66b7a7ab28377192aChris Lattner       ConstantStrings));
20341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::StructType *StaticsListTy =
203547a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson      llvm::StructType::get(VMContext, PtrToInt8Ty, StaticsArrayTy, NULL);
2036a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson    llvm::Type *StaticsListPtrTy =
203796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::PointerType::getUnqual(StaticsListTy);
203871238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
20391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::ArrayType *StaticsListArrayTy =
204096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::ArrayType::get(StaticsListPtrTy, 2);
204171238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Elements.clear();
204271238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Elements.push_back(Statics);
2043c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson    Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
204471238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
20453c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
204671238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  }
204720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of classes, categories, and constant objects
204896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
204920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Classes.size() + Categories.size()  + 2);
20501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::StructType *SymTabTy = llvm::StructType::get(VMContext,
205147a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson                                                     LongTy, SelStructPtrTy,
20520032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                     llvm::Type::getInt16Ty(VMContext),
20530032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                     llvm::Type::getInt16Ty(VMContext),
2054630404b16e886238ed68a1abbfe2e548f43aa664Chris Lattner                                                     ClassListTy, NULL);
205520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
205620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
205720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Pointer to an array of selectors used in this module.
205820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Selectors;
20599f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::vector<llvm::GlobalAlias*> SelectorAliases;
20609f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  for (SelectorMap::iterator iter = SelectorTable.begin(),
20619f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {
20629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20639f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    std::string SelNameStr = iter->first.getAsString();
20649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");
20659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::SmallVectorImpl<TypedSelector> &Types = iter->second;
20679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    for (llvm::SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
20689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        e = Types.end() ; i!=e ; i++) {
20699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      llvm::Constant *SelectorTypeEncoding = NULLPtr;
20719f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      if (!i->first.empty())
20729f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");
20739f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20749f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.push_back(SelName);
20759f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.push_back(SelectorTypeEncoding);
20769f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
20779f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.clear();
20789f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20799f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      // Store the selector alias for later replacement
20809f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      SelectorAliases.push_back(i->second);
20819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    }
208220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
20839f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  unsigned SelectorCount = Selectors.size();
20849f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // NULL-terminate the selector list.  This should not actually be required,
20859f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // because the selector list has a length field.  Unfortunately, the GCC
20869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // runtime decides to ignore the length field and expects a NULL terminator,
20879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // and GCC cooperates with this by always setting the length to 0.
208820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
208920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
209008e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson  Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
209120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
20929f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
209320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of static selectors
20949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount));
20959f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::Constant *SelectorList = MakeGlobalArray(SelStructTy, Selectors,
209620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          ".objc_selector_list");
20971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
2098e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    SelStructPtrTy));
209920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
210020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Now that all of the static selectors exist, create pointers to them.
21019f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  for (unsigned int i=0 ; i<SelectorCount ; i++) {
21029f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
210320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Idxs[] = {Zeros[0],
21049f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), i), Zeros[0]};
21059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    // FIXME: We're generating redundant loads and stores here!
2106c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(SelectorList,
2107c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        Idxs, 2);
2108e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    // If selectors are defined as an opaque type, cast the pointer to this
2109e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    // type.
2110c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
21119f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorAliases[i]->replaceAllUsesWith(SelPtr);
21129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorAliases[i]->eraseFromParent();
211320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
21149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
211520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of classes defined.
21161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
211720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        Classes.size()));
211820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of categories defined
21191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
212020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        Categories.size()));
212120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an array of classes, then categories, then static object instances
212220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.insert(Classes.end(), Categories.begin(), Categories.end());
212320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  //  NULL-terminated list of static object instances (mainly constant strings)
212420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(Statics);
212520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(NULLPtr);
21267db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
212720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ClassList);
21281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Construct the symbol table
212920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
213020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
213120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // The symbol table is contained in a module which has some version-checking
213220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // constants
213347a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson  llvm::StructType * ModuleTy = llvm::StructType::get(VMContext, LongTy, LongTy,
213496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
213520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
21369f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // Runtime version, used for ABI compatibility checking.
21379f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
213891a0b51438e5259a79d68d7450c453827beed9e2Fariborz Jahanian  // sizeof(ModuleTy)
213974a8bbf0654fbbf37be04b240b5bbe591f37f242Benjamin Kramer  llvm::TargetData td(&TheModule);
21404a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(LongTy,
2141a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson                     td.getTypeSizeInBits(ModuleTy)/8));
21429f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
21439f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // The path to the source file where this module was declared
21449f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SourceManager &SM = CGM.getContext().getSourceManager();
21459f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
21469f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::string path =
21479f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
21489f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
21499f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
215020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(SymTab);
215120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
215220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
215320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create the load function calling the runtime entry point with the module
215420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // structure
215520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Function * LoadFunction = llvm::Function::Create(
21560032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
215720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::GlobalValue::InternalLinkage, ".objc_load_function",
215820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      &TheModule);
21590032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::BasicBlock *EntryBB =
21600032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
2161a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson  CGBuilderTy Builder(VMContext);
216220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.SetInsertPoint(EntryBB);
216326c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian
216426c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian  std::vector<const llvm::Type*> Params(1,
216596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::PointerType::getUnqual(ModuleTy));
216696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
21670032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        llvm::Type::getVoidTy(VMContext), Params, true), "__objc_exec_class");
216820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.CreateCall(Register, Module);
216920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.CreateRetVoid();
21709cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian
217120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return LoadFunction;
217220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
21737ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
2174679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianllvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
21751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const ObjCContainerDecl *CD) {
21761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ObjCCategoryImplDecl *OCD =
21773e0a540b6d846178857289ec0eb8470a278d11a3Steve Naroff    dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
21789f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::StringRef CategoryName = OCD ? OCD->getName() : "";
21799f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::StringRef ClassName = CD->getName();
21809f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Selector MethodName = OMD->getSelector();
2181f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  bool isClassMethod = !OMD->isInstanceMethod();
2182391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
2183541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  CodeGenTypes &Types = CGM.getTypes();
21841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::FunctionType *MethodTy =
2185541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
218620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
218720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      MethodName, isClassMethod);
218820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
2189d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar  llvm::Function *Method
21901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = llvm::Function::Create(MethodTy,
21911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             llvm::GlobalValue::InternalLinkage,
21921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             FunctionName,
21931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             &TheModule);
2194391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  return Method;
2195391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner}
2196391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
219749f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbarllvm::Function *CGObjCGNU::GetPropertyGetFunction() {
21989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetPropertyFn;
219949f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar}
220049f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar
220149f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbarllvm::Function *CGObjCGNU::GetPropertySetFunction() {
22029f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return SetPropertyFn;
220349f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar}
220449f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar
22058fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnallllvm::Function *CGObjCGNU::GetGetStructFunction() {
22069f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetStructPropertyFn;
22078fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall}
22088fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnallllvm::Function *CGObjCGNU::GetSetStructFunction() {
22099f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return SetStructPropertyFn;
22106cc590602f41c3e98e8af0023d54296c8eca7910Fariborz Jahanian}
22116cc590602f41c3e98e8af0023d54296c8eca7910Fariborz Jahanian
2212309a4368cd299cff30ab22709e2b772cf8059e45Daniel Dunbarllvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
22139f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return EnumerationMutationFn;
22142abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson}
22152abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson
22169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
2217f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                     const ObjCAtSynchronizedStmt &S) {
22189735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
2219f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
22205dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
22215dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
22229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
2223f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                            const ObjCAtTryStmt &S) {
2224f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Unlike the Apple non-fragile runtimes, which also uses
2225f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // unwind-based zero cost exceptions, the GNU Objective C runtime's
2226f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // EH support isn't a veneer over C++ EH.  Instead, exception
2227f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // objects are created by __objc_exception_throw and destroyed by
2228f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // the personality function; this avoids the need for bracketing
2229f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // catch handlers with calls to __blah_begin_catch/__blah_end_catch
2230f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // (or even _Unwind_DeleteException), but probably doesn't
2231f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // interoperate very well with foreign exceptions.
22329735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  //
223380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // In Objective-C++ mode, we actually emit something equivalent to the C++
22349735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  // exception handler.
22359735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
22369735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  return ;
223764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
223864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
22399f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
224049f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar                              const ObjCAtThrowStmt &S) {
22415dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  llvm::Value *ExceptionAsObject;
22425dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
22435dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  if (const Expr *ThrowExpr = S.getThrowExpr()) {
22445dc0867af17b3bd6f567897433853b2b4767446cChris Lattner    llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
22451e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian    ExceptionAsObject = Exception;
22465dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  } else {
22471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
22485dc0867af17b3bd6f567897433853b2b4767446cChris Lattner           "Unexpected rethrow outside @catch block.");
22495dc0867af17b3bd6f567897433853b2b4767446cChris Lattner    ExceptionAsObject = CGF.ObjCEHValueStack.back();
22505dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  }
22511e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian  ExceptionAsObject =
22521e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian      CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
22531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22545dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // Note: This may have to be an invoke, if we want to support constructs like:
22555dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // @try {
22565dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  //  @throw(obj);
22575dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // }
22585dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // @catch(id) ...
22595dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  //
22605dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // This is effectively turning @throw into an incredibly-expensive goto, but
22615dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // it may happen as a result of inlining followed by missed optimizations, or
22625dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // as a result of stupidity.
22635dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
22645dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  if (!UnwindBB) {
22659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    CGF.Builder.CreateCall(ExceptionThrowFn, ExceptionAsObject);
22665dc0867af17b3bd6f567897433853b2b4767446cChris Lattner    CGF.Builder.CreateUnreachable();
22675dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  } else {
22689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    CGF.Builder.CreateInvoke(ExceptionThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
22695dc0867af17b3bd6f567897433853b2b4767446cChris Lattner        &ExceptionAsObject+1);
22705dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  }
22715dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // Clear the insertion point to indicate we are in unreachable code.
22725dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  CGF.Builder.ClearInsertionPoint();
227364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
227464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
22759f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
22761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          llvm::Value *AddrWeakObj) {
2277ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2278ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  AddrWeakObj = EnforceType(B, AddrWeakObj, IdTy);
2279ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  return B.CreateCall(WeakReadFn, AddrWeakObj);
22806dc2317b59cb1180a59f6c283d96b7a5dfeb5307Fariborz Jahanian}
22816dc2317b59cb1180a59f6c283d96b7a5dfeb5307Fariborz Jahanian
22829f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
22831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                   llvm::Value *src, llvm::Value *dst) {
2284ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2285ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2286ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2287ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall2(WeakAssignFn, src, dst);
22883e283e344595e0bd499b13b30a92b7d9c10a2140Fariborz Jahanian}
22893e283e344595e0bd499b13b30a92b7d9c10a2140Fariborz Jahanian
22909f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
2291021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                     llvm::Value *src, llvm::Value *dst,
2292021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                     bool threadlocal) {
2293ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2294ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2295ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2296021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian  if (!threadlocal)
2297021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian    B.CreateCall2(GlobalAssignFn, src, dst);
2298021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian  else
2299021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian    // FIXME. Add threadloca assign API
2300021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian    assert(false && "EmitObjCGlobalAssign - Threal Local API NYI");
230158626500527695865683d1d65053743de8770b60Fariborz Jahanian}
230258626500527695865683d1d65053743de8770b60Fariborz Jahanian
23039f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
23046c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                   llvm::Value *src, llvm::Value *dst,
23056c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                   llvm::Value *ivarOffset) {
2306ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2307ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2308ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2309ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall3(IvarAssignFn, src, dst, ivarOffset);
23107eda8367cf63caee8acf907356b1d199ccaa6e89Fariborz Jahanian}
23117eda8367cf63caee8acf907356b1d199ccaa6e89Fariborz Jahanian
23129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
23131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                         llvm::Value *src, llvm::Value *dst) {
2314ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2315ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2316ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2317ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall2(StrongCastAssignFn, src, dst);
231858626500527695865683d1d65053743de8770b60Fariborz Jahanian}
231958626500527695865683d1d65053743de8770b60Fariborz Jahanian
23209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
23211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                         llvm::Value *DestPtr,
2322082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian                                         llvm::Value *SrcPtr,
232355bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian                                         llvm::Value *Size) {
2324ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2325ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  DestPtr = EnforceType(B, DestPtr, IdTy);
2326ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  SrcPtr = EnforceType(B, SrcPtr, PtrToIdTy);
2327ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
232855bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian  B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size);
2329082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian}
2330082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian
23319cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanianllvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
23329cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                              const ObjCInterfaceDecl *ID,
23339cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                              const ObjCIvarDecl *Ivar) {
23349cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
23359cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    + '.' + Ivar->getNameAsString();
23369cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // Emit the variable and initialize it with what we think the correct value
23379cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // is.  This allows code compiled with non-fragile ivars to work correctly
23389cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // when linked against code which isn't (most of the time).
23398a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
23408a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  if (!IvarOffsetPointer) {
2341e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // This will cause a run-time crash if we accidentally use it.  A value of
2342e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // 0 would seem more sensible, but will silently overwrite the isa pointer
2343e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // causing a great deal of confusion.
2344e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    uint64_t Offset = -1;
2345e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // We can't call ComputeIvarBaseOffset() here if we have the
2346e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // implementation, because it will create an invalid ASTRecordLayout object
2347e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // that we are then stuck with forever, so we only initialize the ivar
2348e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // offset variable with a guess if we only have the interface.  The
2349e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // initializer will be reset later anyway, when we are generating the class
2350e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // description.
2351e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    if (!CGM.getContext().getObjCImplementation(
2352cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman              const_cast<ObjCInterfaceDecl *>(ID)))
2353d901da531433254210a08e8b1f9e1ad049b340aaDavid Chisnall      Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
2354d901da531433254210a08e8b1f9e1ad049b340aaDavid Chisnall
23559cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    llvm::ConstantInt *OffsetGuess =
2356f950837b8250c7f08d0b7b15ace0b35740721c9dDavid Chisnall      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar");
23578a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // Don't emit the guess in non-PIC code because the linker will not be able
23588a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // to replace it with the real version for a library.  In non-PIC code you
23598a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // must compile with the fragile ABI if you want to use ivars from a
23601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // GCC-compiled class.
23618a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    if (CGM.getLangOptions().PICLevel) {
23628a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
23638a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            llvm::Type::getInt32Ty(VMContext), false,
23648a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
23658a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
23668a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
23678a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            IvarOffsetGV, Name);
23688a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    } else {
23698a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
23703c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer              llvm::Type::getInt32PtrTy(VMContext), false,
23713c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer              llvm::GlobalValue::ExternalLinkage, 0, Name);
23728a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    }
23739cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
23748a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  return IvarOffsetPointer;
23759cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian}
23769cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian
23779f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallLValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
2378598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       QualType ObjectTy,
2379598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       llvm::Value *BaseValue,
2380598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       const ObjCIvarDecl *Ivar,
2381598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       unsigned CVRQualifiers) {
2382c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl *ID =
2383c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjectTy->getAs<ObjCObjectType>()->getInterface();
23849777687562c338601c2f17906e65e1c1a0aad96fDaniel Dunbar  return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
23859777687562c338601c2f17906e65e1c1a0aad96fDaniel Dunbar                                  EmitIvarOffset(CGF, ID, Ivar));
23860bb20361a321593887f067515dd04cf109f4c74aFariborz Jahanian}
2387bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
23889cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanianstatic const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
23899cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                                                  const ObjCInterfaceDecl *OID,
23909cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                                                  const ObjCIvarDecl *OIVD) {
23919cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
23928e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  Context.ShallowCollectObjCIvars(OID, Ivars);
23939cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
23949cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    if (OIVD == Ivars[k])
23959cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      return OID;
23969cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
23971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23989cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // Otherwise check in the super class.
23999cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
24009cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    return FindIvarInterface(Context, Super, OIVD);
24011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24029cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  return 0;
24039cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian}
24040bb20361a321593887f067515dd04cf109f4c74aFariborz Jahanian
24059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
24062a03192a02dbf4fdff438d1e658356bde871aba4Daniel Dunbar                         const ObjCInterfaceDecl *Interface,
2407f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian                         const ObjCIvarDecl *Ivar) {
24088a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  if (CGM.getLangOptions().ObjCNonFragileABI) {
24099cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
24107e02d1aebb3ee0841f398a836ba5d34be5cdb4b3David Chisnall    return CGF.Builder.CreateZExtOrBitCast(
24117e02d1aebb3ee0841f398a836ba5d34be5cdb4b3David Chisnall        CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
24127e02d1aebb3ee0841f398a836ba5d34be5cdb4b3David Chisnall                ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")),
24137e02d1aebb3ee0841f398a836ba5d34be5cdb4b3David Chisnall        PtrDiffTy);
24149cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
24159777687562c338601c2f17906e65e1c1a0aad96fDaniel Dunbar  uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
24167e02d1aebb3ee0841f398a836ba5d34be5cdb4b3David Chisnall  return llvm::ConstantInt::get(PtrDiffTy, Offset, "ivar");
2417f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian}
2418f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian
24199f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCRuntime *
24209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
24219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  if (CGM.getLangOptions().ObjCNonFragileABI)
24229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    return new CGObjCGNUstep(CGM);
24239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return new CGObjCGCC(CGM);
24240f984268b05edab2cc555a427c441baa9c252658Chris Lattner}
2425