CGObjCGNU.cpp revision 3fc81d32314eca98b51846f493a8bc77cf14a701
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
395f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner#include <cstdarg>
40e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
41e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
42dce1406f1c1f572cfd61c494546572d63461c741Chris Lattnerusing namespace clang;
4346f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbarusing namespace CodeGen;
4420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
450f984268b05edab2cc555a427c441baa9c252658Chris Lattner
460f984268b05edab2cc555a427c441baa9c252658Chris Lattnernamespace {
4781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class that lazily initialises the runtime function.  Avoids inserting the
4881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// types and the function declaration into a module if they're not used, and
4981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// avoids constructing the type more than once if it's used more than once.
509f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass LazyRuntimeFunction {
519f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CodeGenModule *CGM;
529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  std::vector<llvm::Type*> ArgTys;
539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const char *FunctionName;
54789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall  llvm::Constant *Function;
559f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
5681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Constructor leaves this class uninitialized, because it is intended to
5781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// be used as a field in another class and not all of the types that are
5881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// used as arguments will necessarily be available at construction time.
599f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    LazyRuntimeFunction() : CGM(0), FunctionName(0), Function(0) {}
609f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
6181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Initialises the lazy function with the name, return type, and the types
6281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// of the arguments.
639f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    END_WITH_NULL
649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    void init(CodeGenModule *Mod, const char *name,
659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner        llvm::Type *RetTy, ...) {
669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       CGM =Mod;
679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       FunctionName = name;
689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       Function = 0;
699735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       ArgTys.clear();
709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_list Args;
719f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_start(Args, RetTy);
729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner         while (llvm::Type *ArgTy = va_arg(Args, llvm::Type*))
739f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall           ArgTys.push_back(ArgTy);
749f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_end(Args);
759f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       // Push the return type on at the end so we can pop it off easily
769f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.push_back(RetTy);
779f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall   }
7881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall   /// Overloaded cast operator, allows the class to be implicitly cast to an
7981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall   /// LLVM constant.
80789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall   operator llvm::Constant*() {
819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     if (!Function) {
829735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       if (0 == FunctionName) return 0;
839735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // We put the return type on the end of the vector, so pop it back off
842acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner       llvm::Type *RetTy = ArgTys.back();
859f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.pop_back();
869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       llvm::FunctionType *FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       Function =
88789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall         cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName));
899735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // We won't need to use the types again, so we may as well clean up the
909735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // vector now
919f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.resize(0);
929f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     }
939f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     return Function;
949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall   }
95789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall   operator llvm::Function*() {
965f0bcc4c3f4a75375a3571e71bffebf603f4ca67David Chisnall     return cast<llvm::Function>((llvm::Constant*)*this);
97789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall   }
985f0bcc4c3f4a75375a3571e71bffebf603f4ca67David Chisnall
999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
1009f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
1019f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
10281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// GNU Objective-C runtime code generation.  This class implements the parts of
10381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Objective-C support that are specific to the GNU family of runtimes (GCC and
10481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// GNUstep).
1059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGNU : public CGObjCRuntime {
106c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprotected:
10781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The module that is using this class
1089f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CodeGenModule &CGM;
10981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The LLVM module into which output is inserted
1100f984268b05edab2cc555a427c441baa9c252658Chris Lattner  llvm::Module &TheModule;
11181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// strut objc_super.  Used for sending messages to super.  This structure
11281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// contains the receiver (object) and the expected class.
1132acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::StructType *ObjCSuperTy;
11481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// struct objc_super*.  The type of the argument to the superclass message
11581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// lookup functions.
1162acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::PointerType *PtrToObjCSuperTy;
11781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for selectors.  Opaque pointer (i8*) unless a header declaring
11881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// SEL is included in a header somewhere, in which case it will be whatever
11981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// type is declared in that header, most likely {i8*, i8*}.
1209cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *SelectorTy;
12181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM i8 type.  Cached here to avoid repeatedly getting it in all of the
12281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// places where it's used
1232acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *Int8Ty;
12481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Pointer to i8 - LLVM type of char*, for all of the places where the
12581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime needs to deal with C strings.
1269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *PtrToInt8Ty;
12781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Instance Method Pointer type.  This is a pointer to a function that takes,
12881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// at a minimum, an object and a selector, and is the generic type for
12981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C methods.  Due to differences between variadic / non-variadic
13081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// calling conventions, it must always be cast to the correct type before
13181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// actually being used.
1329cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *IMPTy;
13381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Type of an untyped Objective-C object.  Clang treats id as a built-in type
13481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
13581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// but if the runtime header declaring it is included then it may be a
13681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pointer to a structure.
1379cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *IdTy;
13881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Pointer to a pointer to an Objective-C object.  Used in the new ABI
13981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// message lookup function and some GC-related functions.
1402acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::PointerType *PtrToIdTy;
14181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The clang type of id.  Used when using the clang CGCall infrastructure to
14281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// call Objective-C methods.
143ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType ASTIdTy;
14481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C int type.
1459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::IntegerType *IntTy;
14681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for an opaque pointer.  This is identical to PtrToInt8Ty, but is
14781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used in the code to document the difference between i8* meaning a pointer
14881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// to a C string and i8* meaning a pointer to some opaque type.
1499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *PtrTy;
15081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C long type.  The runtime uses this in a lot of places where
15181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// it should be using intptr_t, but we can't fix this without breaking
15281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// compatibility with GCC...
153ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::IntegerType *LongTy;
15481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C size_t.  Used in various runtime data structures.
1552acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *SizeTy;
15681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C ptrdiff_t.  Mainly used in property accessor functions.
1572acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *PtrDiffTy;
15881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C int*.  Used for GCC-ABI-compatible non-fragile instance
15981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// variables.
1602acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::PointerType *PtrToIntTy;
16181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for Objective-C BOOL type.
1622acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *BoolTy;
16381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Metadata kind used to tie method lookups to message sends.  The GNUstep
16481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime provides some LLVM passes that can use this to do things like
16581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// automatic IMP caching and speculative inlining.
166c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  unsigned msgSendMDKind;
16781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Helper function that generates a constant string and returns a pointer to
16881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// the start of the string.  The result of this function can be used anywhere
16981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// where the C code specifies const char*.
1709735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  llvm::Constant *MakeConstantString(const std::string &Str,
1719735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                     const std::string &Name="") {
1729735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
173a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad    return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros);
1749735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
17581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Emits a linkonce_odr string, whose name is the prefix followed by the
17681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// string value.  This allows the linker to combine the strings between
17781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// different modules.  Used for EH typeinfo names, selector strings, and a
17881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// few other things.
1799735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  llvm::Constant *ExportUniqueString(const std::string &Str,
1809735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                     const std::string prefix) {
1819735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    std::string name = prefix + Str;
1829735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
1839735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    if (!ConstStr) {
1849735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true);
1859735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
1869735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall              llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
1879735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    }
188a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad    return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros);
1899735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
19081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global structure, initialized by the elements in the vector.
19181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The element types must match the types of the structure elements in the
19281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// first argument.
1932acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::GlobalVariable *MakeGlobal(llvm::StructType *Ty,
1949735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   std::vector<llvm::Constant*> &V,
1955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   StringRef Name="",
1969735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   llvm::GlobalValue::LinkageTypes linkage
1979735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
1989735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
1999735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return new llvm::GlobalVariable(TheModule, Ty, false,
2009735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        linkage, C, Name);
2019735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
20281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global array.  The vector must contain the same number of
20381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// elements that the array type declares, of the type specified as the array
20481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// element type.
2052acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::GlobalVariable *MakeGlobal(llvm::ArrayType *Ty,
2069735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   std::vector<llvm::Constant*> &V,
2075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   StringRef Name="",
2089735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   llvm::GlobalValue::LinkageTypes linkage
2099735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
2109735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
2119735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return new llvm::GlobalVariable(TheModule, Ty, false,
2129735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                    linkage, C, Name);
2139735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
21481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global array, inferring the array type from the specified
21581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// element type and the size of the initialiser.
2162acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::GlobalVariable *MakeGlobalArray(llvm::Type *Ty,
2179735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                        std::vector<llvm::Constant*> &V,
2185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                        StringRef Name="",
2199735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                        llvm::GlobalValue::LinkageTypes linkage
2209735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
2219735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size());
2229735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return MakeGlobal(ArrayTy, V, Name, linkage);
2239735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
22481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Ensures that the value has the required type, by inserting a bitcast if
22581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// required.  This function lets us avoid inserting bitcasts that are
22681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// redundant.
2272acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Value* EnforceType(CGBuilderTy B, llvm::Value *V, llvm::Type *Ty){
228c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    if (V->getType() == Ty) return V;
229c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    return B.CreateBitCast(V, Ty);
230c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
231c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // Some zeros used for GEPs in lots of places.
232c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Constant *Zeros[2];
23381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Null pointer value.  Mainly used as a terminator in various arrays.
234c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Constant *NULLPtr;
23581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM context.
236c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::LLVMContext &VMContext;
237c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprivate:
23881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Placeholder for the class.  Lots of things refer to the class before we've
23981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// actually emitted it.  We use this alias as a placeholder, and then replace
24081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// it with a pointer to the class structure before finally emitting the
24181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// module.
2425efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  llvm::GlobalAlias *ClassPtrAlias;
24381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Placeholder for the metaclass.  Lots of things refer to the class before
24481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// we've / actually emitted it.  We use this alias as a placeholder, and then
24581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// replace / it with a pointer to the metaclass structure before finally
24681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// emitting the / module.
2475efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  llvm::GlobalAlias *MetaClassPtrAlias;
24881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the classes that have been generated for this compilation units.
24920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Classes;
25081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the categories that have been generated for this compilation units.
25120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Categories;
25281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the Objective-C constant strings that have been generated for this
25381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// compilation units.
25420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> ConstantStrings;
25581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Map from string values to Objective-C constant strings in the output.
25681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Used to prevent emitting Objective-C strings more than once.  This should
25781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// not be required at all - CodeGenModule should manage this list.
25848272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  llvm::StringMap<llvm::Constant*> ObjCStrings;
25981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the protocols that have been declared.
26020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::StringMap<llvm::Constant*> ExistingProtocols;
26181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// For each variant of a selector, we store the type encoding and a
26281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// placeholder value.  For an untyped selector, the type will be the empty
26381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// string.  Selector references are all done via the module's selector table,
26481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// so we create an alias as a placeholder and then replace it with the real
26581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// value later.
2669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
26781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Type of the selector map.  This is roughly equivalent to the structure
26881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used in the GNUstep runtime, which maintains a list of all of the valid
26981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// types for a selector in a table.
2705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> >
2719f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorMap;
27281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// A map from selectors to selector types.  This allows us to emit all
27381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// selectors of the same name and type together.
2749f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SelectorMap SelectorTable;
2759f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
27681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Selectors related to memory management.  When compiling in GC mode, we
27781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// omit these.
278ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  Selector RetainSel, ReleaseSel, AutoreleaseSel;
27981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Runtime functions used for memory management in GC mode.  Note that clang
28081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// supports code generation for calling these functions, but neither GNU
28181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime actually supports this API properly yet.
2829f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
2839f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    WeakAssignFn, GlobalAssignFn;
2849f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
2859735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnallprotected:
28681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for throwing Objective-C exceptions.
2879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction ExceptionThrowFn;
28881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for rethrowing exceptions, used at the end of @finally or
28981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// @synchronize blocks.
2909735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction ExceptionReThrowFn;
29181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when entering a catch function.  This is required for
29281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// differentiating Objective-C exceptions and foreign exceptions.
2939735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction EnterCatchFn;
29481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when exiting from a catch block.  Used to do exception
29581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// cleanup.
2969735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction ExitCatchFn;
29781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when entering an @synchronize block.  Acquires the lock.
2989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SyncEnterFn;
29981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when exiting an @synchronize block.  Releases the lock.
3009f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SyncExitFn;
3019f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
3029735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnallprivate:
3039735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall
30481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called if fast enumeration detects that the collection is
30581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// modified during the update.
3069f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction EnumerationMutationFn;
30781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function for implementing synthesized property getters that return an
30881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// object.
3099f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction GetPropertyFn;
31081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function for implementing synthesized property setters that return an
31181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// object.
3129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SetPropertyFn;
31381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for non-object declared property getters.
3149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction GetStructPropertyFn;
31581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for non-object declared property setters.
3169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SetStructPropertyFn;
3179f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
31881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The version of the runtime that this class targets.  Must match the
31981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// version in the runtime.
320a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall  int RuntimeVersion;
32181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The version of the protocol class.  Used to differentiate between ObjC1
32281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and ObjC2 protocols.  Objective-C 1 protocols can not contain optional
32381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// components and can not contain declared properties.  We always emit
32481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C 2 property structures, but we have to pretend that they're
32581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C 1 property structures when targeting the GCC runtime or it
32681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// will abort.
3279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const int ProtocolVersion;
32820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovprivate:
32981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates an instance variable list structure.  This is a structure
33081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// containing a size and an array of structures containing instance variable
33181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// metadata.  This is used purely for introspection in the fragile ABI.  In
33281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// the non-fragile ABI, it's used for instance variable fixup.
33320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateIvarList(
3345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const SmallVectorImpl<llvm::Constant *>  &IvarNames,
3355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const SmallVectorImpl<llvm::Constant *>  &IvarTypes,
3365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const SmallVectorImpl<llvm::Constant *>  &IvarOffsets);
33781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a method list structure.  This is a structure containing a size
33881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and an array of structures containing method metadata.
33981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  ///
34081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// This structure is used by both classes and categories, and contains a next
34181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pointer allowing them to be chained together in a linked list.
3425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  llvm::Constant *GenerateMethodList(const StringRef &ClassName,
3435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const StringRef &CategoryName,
3445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const SmallVectorImpl<Selector>  &MethodSels,
3455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const SmallVectorImpl<llvm::Constant *>  &MethodTypes,
34620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      bool isClassMethodList);
34781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Emits an empty protocol.  This is used for @protocol() where no protocol
34881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// is found.  The runtime will (hopefully) fix up the pointer to refer to the
34981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// real protocol.
350f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
35181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a list of property metadata structures.  This follows the same
35281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pattern as method and instance variable metadata lists.
353d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
3545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVectorImpl<Selector> &InstanceMethodSels,
3555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
35681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a list of referenced protocols.  Classes, categories, and
35781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// protocols all use this structure.
35820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateProtocolList(
3595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const SmallVectorImpl<std::string> &Protocols);
36081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// To ensure that all protocols are seen by the runtime, we add a category on
36181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// a class defined in the runtime, declaring no methods, but adopting the
36281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// protocols.  This is a horribly ugly hack, but it allows us to collect all
36381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// of the protocols without changing the ABI.
364d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  void GenerateProtocolHolderCategory(void);
36581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a class structure.
36620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateClassStructure(
36720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *MetaClass,
36820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *SuperClass,
36920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      unsigned info,
370d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner      const char *Name,
37120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *Version,
37220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *InstanceSize,
37320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *IVars,
37420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *Methods,
375d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *Protocols,
376d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *IvarOffsets,
3778c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall      llvm::Constant *Properties,
3788c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall      bool isMeta=false);
37981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a method list.  This is used by protocols to define the required
38081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and optional methods.
38120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateProtocolMethodList(
3825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const SmallVectorImpl<llvm::Constant *>  &MethodNames,
3835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const SmallVectorImpl<llvm::Constant *>  &MethodTypes);
38481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Returns a selector with the specified type encoding.  An empty string is
38581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used to return an untyped selector (with the types field set to NULL).
3869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
3879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    const std::string &TypeEncoding, bool lval);
38881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Returns the variable used to store the offset of an instance variable.
3899cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
3909cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      const ObjCIvarDecl *Ivar);
39181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Emits a reference to a class.  This allows the linker to object if there
39281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// is no class of the matching name.
3932a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  void EmitClassRef(const std::string &className);
394c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  /// Emits a pointer to the named class
395d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall  llvm::Value *GetClassNamed(CGBuilderTy &Builder, const std::string &Name,
396d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall                             bool isWeak);
397c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprotected:
39881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Looks up the method for sending a message to the specified object.  This
39981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// mechanism differs between the GCC and GNU runtimes, so this method must be
40081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// overridden in subclasses.
401c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
402c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *&Receiver,
403c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *cmd,
404c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::MDNode *node) = 0;
40581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Looks up the method for sending a message to a superclass.  This mechanism
40681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// differs between the GCC and GNU runtimes, so this method must be
40781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// overridden in subclasses.
408c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
409c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *ObjCSuper,
410c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *cmd) = 0;
4110f984268b05edab2cc555a427c441baa9c252658Chris Lattnerpublic:
4129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
4139f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      unsigned protocolClassVersion);
4149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
4150d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  virtual llvm::Constant *GenerateConstantString(const StringLiteral *);
4169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
4179f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual RValue
4189f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GenerateMessageSend(CodeGenFunction &CGF,
419ef072fd2f3347cfd857d6eb787b245b950771430John McCall                      ReturnValueSlot Return,
4207f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                      QualType ResultType,
4217f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                      Selector Sel,
422f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                      llvm::Value *Receiver,
423df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                      const CallArgList &CallArgs,
424c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                      const ObjCInterfaceDecl *Class,
425df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                      const ObjCMethodDecl *Method);
4269f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual RValue
4279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GenerateMessageSendSuper(CodeGenFunction &CGF,
428ef072fd2f3347cfd857d6eb787b245b950771430John McCall                           ReturnValueSlot Return,
4297f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                           QualType ResultType,
4307f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                           Selector Sel,
431f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                           const ObjCInterfaceDecl *Class,
4327ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian                           bool isCategoryImpl,
433f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                           llvm::Value *Receiver,
43419cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar                           bool IsClassMessage,
435d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                           const CallArgList &CallArgs,
436d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                           const ObjCMethodDecl *Method);
43745d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
438ddb2a3d55a24a1dbdf9152621642d9a4b4fc2f61Daniel Dunbar                                const ObjCInterfaceDecl *OID);
43903b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanian  virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
44003b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanian                                   bool lval = false);
4416d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbar  virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
4426d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbar      *Method);
443cf5abc7ba032bd35158e4d75b0bc92a482fc67e8Fariborz Jahanian  virtual llvm::Constant *GetEHType(QualType T);
4441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
446679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian                                         const ObjCContainerDecl *CD);
4477ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
4487ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
44945d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
450af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar                                           const ObjCProtocolDecl *PD);
451af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
45220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  virtual llvm::Function *ModuleInitFunction();
453789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall  virtual llvm::Constant *GetPropertyGetFunction();
454789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall  virtual llvm::Constant *GetPropertySetFunction();
455789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall  virtual llvm::Constant *GetSetStructFunction();
456789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall  virtual llvm::Constant *GetGetStructFunction();
457309a4368cd299cff30ab22709e2b772cf8059e45Daniel Dunbar  virtual llvm::Constant *EnumerationMutationFunction();
4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4599f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitTryStmt(CodeGenFunction &CGF,
460f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                           const ObjCAtTryStmt &S);
4619f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitSynchronizedStmt(CodeGenFunction &CGF,
462f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                    const ObjCAtSynchronizedStmt &S);
4639f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitThrowStmt(CodeGenFunction &CGF,
46464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson                             const ObjCAtThrowStmt &S);
4659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
4666dc2317b59cb1180a59f6c283d96b7a5dfeb5307Fariborz Jahanian                                         llvm::Value *AddrWeakObj);
4679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitObjCWeakAssign(CodeGenFunction &CGF,
4683e283e344595e0bd499b13b30a92b7d9c10a2140Fariborz Jahanian                                  llvm::Value *src, llvm::Value *dst);
4699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitObjCGlobalAssign(CodeGenFunction &CGF,
470021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                    llvm::Value *src, llvm::Value *dest,
471021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                    bool threadlocal=false);
4729f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitObjCIvarAssign(CodeGenFunction &CGF,
4736c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                    llvm::Value *src, llvm::Value *dest,
4746c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                    llvm::Value *ivarOffset);
4759f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
47658626500527695865683d1d65053743de8770b60Fariborz Jahanian                                        llvm::Value *src, llvm::Value *dest);
4779f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual void EmitGCMemmoveCollectable(CodeGenFunction &CGF,
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        llvm::Value *DestPtr,
479082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian                                        llvm::Value *SrcPtr,
48055bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian                                        llvm::Value *Size);
4819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual LValue EmitObjCValueForIvar(CodeGenFunction &CGF,
482598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                      QualType ObjectTy,
483598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                      llvm::Value *BaseValue,
484598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                      const ObjCIvarDecl *Ivar,
485598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                      unsigned CVRQualifiers);
4869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
4872a03192a02dbf4fdff438d1e658356bde871aba4Daniel Dunbar                                      const ObjCInterfaceDecl *Interface,
488f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian                                      const ObjCIvarDecl *Ivar);
489c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
4909f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  virtual llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
4916b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                             const CGBlockInfo &blockInfo) {
49289ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian    return NULLPtr;
49389ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian  }
4946f40e2244b0590f144c5ceee07981a962fbbc72eFariborz Jahanian
4956f40e2244b0590f144c5ceee07981a962fbbc72eFariborz Jahanian  virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
4966f40e2244b0590f144c5ceee07981a962fbbc72eFariborz Jahanian    return 0;
4976f40e2244b0590f144c5ceee07981a962fbbc72eFariborz Jahanian  }
4980f984268b05edab2cc555a427c441baa9c252658Chris Lattner};
49981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class representing the legacy GCC Objective-C ABI.  This is the default when
50081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// -fobjc-nonfragile-abi is not specified.
50181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall///
50281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// The GCC ABI target actually generates code that is approximately compatible
50381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// with the new GNUstep runtime ABI, but refrains from using any features that
50481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// would not work with the GCC runtime.  For example, clang always generates
50581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// the extended form of the class structure, and the extra fields are simply
50681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// ignored by GCC libobjc.
5079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGCC : public CGObjCGNU {
50881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The GCC ABI message lookup function.  Returns an IMP pointing to the
50981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// method implementation for this message.
510c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  LazyRuntimeFunction MsgLookupFn;
51181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The GCC ABI superclass message lookup function.  Takes a pointer to a
51281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// structure describing the receiver and the class, and a selector as
51381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// arguments.  Returns the IMP for the corresponding method.
514c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  LazyRuntimeFunction MsgLookupSuperFn;
515c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprotected:
516c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
517c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *&Receiver,
518c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *cmd,
519c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::MDNode *node) {
520c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGBuilderTy &Builder = CGF.Builder;
521c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Value *imp = Builder.CreateCall2(MsgLookupFn,
522c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall            EnforceType(Builder, Receiver, IdTy),
523c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall            EnforceType(Builder, cmd, SelectorTy));
524c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    cast<llvm::CallInst>(imp)->setMetadata(msgSendMDKind, node);
525c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    return imp;
526c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
527c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
528c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *ObjCSuper,
529c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *cmd) {
530c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
531c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
532c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          PtrToObjCSuperTy), cmd};
5334c7d9f1507d0f102bd4133bba63348636facd469Jay Foad      return Builder.CreateCall(MsgLookupSuperFn, lookupArgs);
534c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
5359f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
536c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
537c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // IMP objc_msg_lookup(id, SEL);
538c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, NULL);
539c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // IMP objc_msg_lookup_super(struct objc_super*, SEL);
540c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
541c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              PtrToObjCSuperTy, SelectorTy, NULL);
542c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
5439f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
54481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class used when targeting the new GNUstep runtime ABI.
5459f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGNUstep : public CGObjCGNU {
54681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// The slot lookup function.  Returns a pointer to a cacheable structure
54781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// that contains (among other things) the IMP.
548c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    LazyRuntimeFunction SlotLookupFn;
54981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// The GNUstep ABI superclass message lookup function.  Takes a pointer to
55081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// a structure describing the receiver and the class, and a selector as
55181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// arguments.  Returns the slot for the corresponding method.  Superclass
55281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// message lookup rarely changes, so this is a good caching opportunity.
553c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    LazyRuntimeFunction SlotLookupSuperFn;
55481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Type of an slot structure pointer.  This is returned by the various
55581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// lookup functions.
556c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Type *SlotTy;
557c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  protected:
558c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
559c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                   llvm::Value *&Receiver,
560c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                   llvm::Value *cmd,
561c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                   llvm::MDNode *node) {
562c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
563c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Function *LookupFn = SlotLookupFn;
564c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
565c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Store the receiver on the stack so that we can reload it later
566c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
567c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      Builder.CreateStore(Receiver, ReceiverPtr);
568c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
569c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *self;
570c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
571c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
572c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        self = CGF.LoadObjCSelf();
573c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      } else {
574c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        self = llvm::ConstantPointerNull::get(IdTy);
575c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      }
576c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
577c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // The lookup function is guaranteed not to capture the receiver pointer.
578c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      LookupFn->setDoesNotCapture(1);
579c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
580c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::CallInst *slot =
581c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          Builder.CreateCall3(LookupFn,
582c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              EnforceType(Builder, ReceiverPtr, PtrToIdTy),
583c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              EnforceType(Builder, cmd, SelectorTy),
584c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              EnforceType(Builder, self, IdTy));
585c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      slot->setOnlyReadsMemory();
586c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      slot->setMetadata(msgSendMDKind, node);
587c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
588c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Load the imp from the slot
589c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
590c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
591c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // The lookup function may have changed the receiver, so make sure we use
592c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // the new one.
593c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      Receiver = Builder.CreateLoad(ReceiverPtr, true);
594c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      return imp;
595c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
596c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
597c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                        llvm::Value *ObjCSuper,
598c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                        llvm::Value *cmd) {
599c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
600c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
601c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
6024c7d9f1507d0f102bd4133bba63348636facd469Jay Foad      llvm::CallInst *slot = Builder.CreateCall(SlotLookupSuperFn, lookupArgs);
603c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      slot->setOnlyReadsMemory();
604c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
605c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      return Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
606c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
6079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
608c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) {
6097650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner      llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy,
610c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          PtrTy, PtrTy, IntTy, IMPTy, NULL);
611c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
612c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
613c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
614c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          SelectorTy, IdTy, NULL);
615c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Slot_t objc_msg_lookup_super(struct objc_super*, SEL);
616c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
617c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              PtrToObjCSuperTy, SelectorTy, NULL);
6189735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      // If we're in ObjC++ mode, then we want to make
6199735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      if (CGM.getLangOptions().CPlusPlus) {
6209cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner        llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
6219735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void *__cxa_begin_catch(void *e)
6229735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, NULL);
6239735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void __cxa_end_catch(void)
6249735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        EnterCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, NULL);
6259735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void _Unwind_Resume_or_Rethrow(void*)
626978d415adab4ef423f5aef430ddfdbefc4cfb000David Chisnall        ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy, PtrTy, NULL);
6279735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      }
628c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
6299f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
6309f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
6310f984268b05edab2cc555a427c441baa9c252658Chris Lattner} // end anonymous namespace
6320f984268b05edab2cc555a427c441baa9c252658Chris Lattner
63320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
6342a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// Emits a reference to a dummy variable which is emitted with each class.
6352a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// This ensures that a linker error will be generated when trying to link
6362a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// together modules where a referenced class is not defined.
637bb1c8600218b3244340331165fc7cba7bf227655Mike Stumpvoid CGObjCGNU::EmitClassRef(const std::string &className) {
6382a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  std::string symbolRef = "__objc_class_ref_" + className;
6392a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // Don't emit two copies of the same symbol
640bb1c8600218b3244340331165fc7cba7bf227655Mike Stump  if (TheModule.getGlobalVariable(symbolRef))
641bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    return;
6422a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  std::string symbolName = "__objc_class_name_" + className;
6432a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
6442a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  if (!ClassSymbol) {
6451c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
6461c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson        llvm::GlobalValue::ExternalLinkage, 0, symbolName);
6472a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  }
6481c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson  new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
649f35271b8677a5f31498b89587b1da42a06dd46fdChris Lattner    llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
6502a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner}
65120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
6525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerstatic std::string SymbolNameForMethod(const StringRef &ClassName,
6535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const StringRef &CategoryName, const Selector MethodName,
6549f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    bool isClassMethod) {
6559f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::string MethodNameColonStripped = MethodName.getAsString();
656d346736dfe5ef584a2d0c9d27d217408ee394b9bDavid Chisnall  std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
657d346736dfe5ef584a2d0c9d27d217408ee394b9bDavid Chisnall      ':', '_');
6585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
6599f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    CategoryName + "_" + MethodNameColonStripped).str();
66087935a8d5c0eeda8ad62815f4fc815ad25c7cd8eDavid Chisnall}
66120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
6629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
6639f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    unsigned protocolClassVersion)
664c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  : CGM(cgm), TheModule(CGM.getModule()), VMContext(cgm.getLLVMContext()),
665c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  ClassPtrAlias(0), MetaClassPtrAlias(0), RuntimeVersion(runtimeABIVersion),
666c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  ProtocolVersion(protocolClassVersion) {
667c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall
668c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall  msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
669c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall
6709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CodeGenTypes &Types = CGM.getTypes();
671e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  IntTy = cast<llvm::IntegerType>(
6729f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().IntTy));
673e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  LongTy = cast<llvm::IntegerType>(
6749f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().LongTy));
6758fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  SizeTy = cast<llvm::IntegerType>(
6769f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().getSizeType()));
6778fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  PtrDiffTy = cast<llvm::IntegerType>(
6789f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().getPointerDiffType()));
6798fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
681d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Int8Ty = llvm::Type::getInt8Ty(VMContext);
682d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // C string type.  Used in lots of places.
683d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
684d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
6854a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
68620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Zeros[1] = Zeros[0];
687d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
688391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Get the selector Type.
6890d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  QualType selTy = CGM.getContext().getObjCSelType();
6900d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  if (QualType() == selTy) {
6910d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    SelectorTy = PtrToInt8Ty;
6920d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  } else {
6930d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
6940d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  }
695e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
69696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
697391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  PtrTy = PtrToInt8Ty;
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
699391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Object type
7007bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall  QualType UnqualIdTy = CGM.getContext().getObjCIdType();
7017bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall  ASTIdTy = CanQualType();
7027bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall  if (UnqualIdTy != QualType()) {
7037bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall    ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy);
7040d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
7057bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall  } else {
7067bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall    IdTy = PtrToInt8Ty;
7070d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  }
708ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7107650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, NULL);
711c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
712c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
7139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
7149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7159f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_exception_throw(id);
7169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL);
7179735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL);
7189f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // int objc_sync_enter(id);
7199f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, NULL);
7209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // int objc_sync_exit(id);
7219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, NULL);
7229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_enumerationMutation (id)
7249f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy,
7259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      IdTy, NULL);
7269f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
7289f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
7299f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      PtrDiffTy, BoolTy, NULL);
7309f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
7319f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
7329f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      PtrDiffTy, IdTy, BoolTy, BoolTy, NULL);
7339f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
7349f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
7359f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      PtrDiffTy, BoolTy, BoolTy, NULL);
7369f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
7379f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
7389f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      PtrDiffTy, BoolTy, BoolTy, NULL);
7399f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
740391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // IMP type
7419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *IMPArgs[] = { IdTy, SelectorTy };
742c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
743c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              true));
744ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
745f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall  const LangOptions &Opts = CGM.getLangOptions();
746f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall  if ((Opts.getGCMode() != LangOptions::NonGC) || Opts.ObjCAutoRefCount)
747f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall    RuntimeVersion = 10;
748f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall
7499735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  // Don't bother initialising the GC stuff unless we're compiling in GC mode
750f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall  if (Opts.getGCMode() != LangOptions::NonGC) {
751a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall    // This is a bit of an hack.  We should sort this out by having a proper
752a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall    // CGObjCGNUstep subclass for GC, but we may want to really support the old
753a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall    // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now
754ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // Get selectors needed in GC mode
755ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    RetainSel = GetNullarySelector("retain", CGM.getContext());
756ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    ReleaseSel = GetNullarySelector("release", CGM.getContext());
757ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
758ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
759ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // Get functions needed in GC mode
760ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
761ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_ivar(id, id, ptrdiff_t);
7629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy,
7639f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        NULL);
764ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_strongCast (id, id*)
7659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
7669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        PtrToIdTy, NULL);
767ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_global(id, id*);
7689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy,
7699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        NULL);
770ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_weak(id, id*);
7719f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, NULL);
772ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_read_weak(id*);
7739f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, NULL);
774ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // void *objc_memmove_collectable(void*, void *, size_t);
7759f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
7769f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        SizeTy, NULL);
777ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
77820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
779bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
780c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnallllvm::Value *CGObjCGNU::GetClassNamed(CGBuilderTy &Builder,
781d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall                                      const std::string &Name,
782d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall                                      bool isWeak) {
783c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  llvm::Value *ClassName = CGM.GetAddrOfConstantCString(Name);
78441d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // With the incompatible ABI, this will need to be replaced with a direct
78541d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // reference to the class symbol.  For the compatible nonfragile ABI we are
78641d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // still performing this lookup at run time but emitting the symbol for the
78741d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // class externally so that we can make the switch later.
788c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  //
789c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class
790c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  // with memoized versions or with static references if it's safe to do so.
791d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall  if (!isWeak)
792d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall    EmitClassRef(Name);
793ddb2a3d55a24a1dbdf9152621642d9a4b4fc2f61Daniel Dunbar  ClassName = Builder.CreateStructGEP(ClassName, 0);
794ddb2a3d55a24a1dbdf9152621642d9a4b4fc2f61Daniel Dunbar
79520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassLookupFn =
796da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true),
79726c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian                              "objc_lookup_class");
79820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return Builder.CreateCall(ClassLookupFn, ClassName);
799391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner}
800391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
801c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall// This has to perform the lookup every time, since posing and related
802c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall// techniques can modify the name -> class mapping.
803c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnallllvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
804c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall                                 const ObjCInterfaceDecl *OID) {
805d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall  return GetClassNamed(Builder, OID->getNameAsString(), OID->isWeakImported());
806c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall}
807c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnallllvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
808d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall  return GetClassNamed(Builder, "NSAutoreleasePool", false);
809c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall}
810c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall
81103b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanianllvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel,
8129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    const std::string &TypeEncoding, bool lval) {
8139f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
8145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypedSelector, 2> &Types = SelectorTable[Sel];
8159f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::GlobalAlias *SelValue = 0;
8169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
8179f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
8185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
8199f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      e = Types.end() ; i!=e ; i++) {
8209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    if (i->first == TypeEncoding) {
8219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      SelValue = i->second;
8229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      break;
8239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    }
8249f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  }
8259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  if (0 == SelValue) {
826c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    SelValue = new llvm::GlobalAlias(SelectorTy,
8279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                     llvm::GlobalValue::PrivateLinkage,
8289f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                     ".objc_selector_"+Sel.getAsString(), NULL,
8299f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                     &TheModule);
8309f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Types.push_back(TypedSelector(TypeEncoding, SelValue));
8319f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  }
8329f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
833c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  if (lval) {
834c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Value *tmp = Builder.CreateAlloca(SelValue->getType());
835c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    Builder.CreateStore(SelValue, tmp);
836c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    return tmp;
837c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
838c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  return SelValue;
8399f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall}
8409f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
8419f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel,
8429f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                    bool lval) {
8439f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetSelector(Builder, Sel, std::string(), lval);
844df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian}
845df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian
8466d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbarllvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
847df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian    *Method) {
848df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  std::string SelTypes;
849df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
8509f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetSelector(Builder, Method->getSelector(), SelTypes, false);
8518e67b63530b4f39a48bc12d97376f373a6901279Chris Lattner}
8528e67b63530b4f39a48bc12d97376f373a6901279Chris Lattner
853cf5abc7ba032bd35158e4d75b0bc92a482fc67e8Fariborz Jahanianllvm::Constant *CGObjCGNU::GetEHType(QualType T) {
8549735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  if (!CGM.getLangOptions().CPlusPlus) {
8559735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      if (T->isObjCIdType()
8569735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall          || T->isObjCQualifiedIdType()) {
8579735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // With the old ABI, there was only one kind of catchall, which broke
8589735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // foreign exceptions.  With the new ABI, we use __objc_id_typeinfo as
8599735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // a pointer indicating object catchalls, and NULL to indicate real
8609735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // catchalls
8619735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        if (CGM.getLangOptions().ObjCNonFragileABI) {
8629735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall          return MakeConstantString("@id");
8639735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        } else {
8649735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall          return 0;
8659735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        }
8669735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      }
8679735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall
8689735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      // All other types should be Objective-C interface pointer types.
8699735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      const ObjCObjectPointerType *OPT =
8709735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        T->getAs<ObjCObjectPointerType>();
8719735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      assert(OPT && "Invalid @catch type.");
8729735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      const ObjCInterfaceDecl *IDecl =
8739735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        OPT->getObjectType()->getInterface();
8749735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      assert(IDecl && "Invalid @catch type.");
8759735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      return MakeConstantString(IDecl->getIdentifier()->getName());
8769735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
87780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // For Objective-C++, we want to provide the ability to catch both C++ and
87880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Objective-C objects in the same function.
87980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
88080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // There's a particular fixed type info for 'id'.
88180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  if (T->isObjCIdType() ||
88280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      T->isObjCQualifiedIdType()) {
88380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    llvm::Constant *IDEHType =
88480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      CGM.getModule().getGlobalVariable("__objc_id_type_info");
88580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    if (!IDEHType)
88680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      IDEHType =
88780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall        new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
88880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall                                 false,
88980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall                                 llvm::GlobalValue::ExternalLinkage,
89080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall                                 0, "__objc_id_type_info");
89180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
89280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  }
89380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
89480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const ObjCObjectPointerType *PT =
89580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    T->getAs<ObjCObjectPointerType>();
89680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  assert(PT && "Invalid @catch type.");
89780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const ObjCInterfaceType *IT = PT->getInterfaceType();
89880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  assert(IT && "Invalid @catch type.");
89980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::string className = IT->getDecl()->getIdentifier()->getName();
90080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
90180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::string typeinfoName = "__objc_eh_typeinfo_" + className;
90280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
90380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Return the existing typeinfo if it exists
90480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
90580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  if (typeinfo) return typeinfo;
90680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
90780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Otherwise create it.
90880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
90980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // vtable for gnustep::libobjc::__objc_class_type_info
91080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // It's quite ugly hard-coding this.  Ideally we'd generate it using the host
91180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // platform's name mangling.
91280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
91380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *Vtable = TheModule.getGlobalVariable(vtableName);
91480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  if (!Vtable) {
91580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
91680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall            llvm::GlobalValue::ExternalLinkage, 0, vtableName);
91780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  }
91880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
919a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad  Vtable = llvm::ConstantExpr::getGetElementPtr(Vtable, Two);
92080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  Vtable = llvm::ConstantExpr::getBitCast(Vtable, PtrToInt8Ty);
92180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
92280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *typeName =
92380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    ExportUniqueString(className, "__objc_eh_typename_");
92480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
92580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::vector<llvm::Constant*> fields;
92680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  fields.push_back(Vtable);
92780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  fields.push_back(typeName);
92880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *TI =
9297650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner      MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
93080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall              NULL), fields, "__objc_eh_typeinfo_" + className,
93180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall          llvm::GlobalValue::LinkOnceODRLinkage);
93280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
9335a180397870944548aaadeaebf58e415885b9489John McCall}
9345a180397870944548aaadeaebf58e415885b9489John McCall
93520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generate an NSConstantString object.
9360d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnallllvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
93748272a078e54d501cb05edd797c54b8e82ceb520David Chisnall
9382f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer  std::string Str = SL->getString().str();
9390d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall
94048272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  // Look for an existing one
94148272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
94248272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  if (old != ObjCStrings.end())
94348272a078e54d501cb05edd797c54b8e82ceb520David Chisnall    return old->getValue();
94448272a078e54d501cb05edd797c54b8e82ceb520David Chisnall
94520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Ivars;
94620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Ivars.push_back(NULLPtr);
94713fd7e5111032f54b538dd66d035b0ccc1f82467Chris Lattner  Ivars.push_back(MakeConstantString(Str));
9484a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
94920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ObjCStr = MakeGlobal(
9507650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
95120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Ivars, ".objc_str");
95248272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
95348272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ObjCStrings[Str] = ObjCStr;
95448272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ConstantStrings.push_back(ObjCStr);
95520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return ObjCStr;
95620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
95720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
95820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///Generates a message send where the super is the receiver.  This is a message
95920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///send to self with special delivery semantics indicating which class's method
96020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///should be called.
9619f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallRValue
9629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
963ef072fd2f3347cfd857d6eb787b245b950771430John McCall                                    ReturnValueSlot Return,
9647f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                                    QualType ResultType,
9657f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                                    Selector Sel,
966f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                                    const ObjCInterfaceDecl *Class,
9677ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian                                    bool isCategoryImpl,
968f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                                    llvm::Value *Receiver,
96919cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar                                    bool IsClassMessage,
970d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                                    const CallArgList &CallArgs,
971d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                                    const ObjCMethodDecl *Method) {
9720bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall  CGBuilderTy &Builder = CGF.Builder;
973e6a11a62d1d66d10a87d81ed697348d66d59adf7David Chisnall  if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) {
974ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == RetainSel || Sel == AutoreleaseSel) {
9750bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall      return RValue::get(EnforceType(Builder, Receiver,
9760bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall                  CGM.getTypes().ConvertType(ResultType)));
977ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
978ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == ReleaseSel) {
979ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall      return RValue::get(0);
980ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
981ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
982db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
983db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  llvm::Value *cmd = GetSelector(Builder, Sel);
984db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
985b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
986b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  CallArgList ActualArgs;
987b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
98804c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
98904c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
990f85e193739c953358c865005855253af4f68a497John McCall  ActualArgs.addFrom(CallArgs);
991b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
992b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  CodeGenTypes &Types = CGM.getTypes();
99304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
994264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                       FunctionType::ExtInfo());
995b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
9965efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  llvm::Value *ReceiverClass = 0;
99748e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner  if (isCategoryImpl) {
99848e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    llvm::Constant *classLookupFunction = 0;
99948e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    if (IsClassMessage)  {
100096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1001da549e8995c447542d5631b8b67fcc3a9582797aJay Foad            IdTy, PtrTy, true), "objc_get_meta_class");
100248e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    } else {
100396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1004da549e8995c447542d5631b8b67fcc3a9582797aJay Foad            IdTy, PtrTy, true), "objc_get_class");
10055efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    }
1006db831948f32217ece8410099f9982ef900c3b3c3David Chisnall    ReceiverClass = Builder.CreateCall(classLookupFunction,
100748e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner        MakeConstantString(Class->getNameAsString()));
10085efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  } else {
100948e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // Set up global aliases for the metaclass or class pointer if they do not
101048e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // already exist.  These will are forward-references which will be set to
1011bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    // pointers to the class and metaclass structure created for the runtime
1012bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    // load function.  To send a message to super, we look up the value of the
101348e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // super_class pointer from either the class or metaclass structure.
101448e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    if (IsClassMessage)  {
101548e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      if (!MetaClassPtrAlias) {
101648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner        MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
101748e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
101848e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            Class->getNameAsString(), NULL, &TheModule);
101948e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      }
102048e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      ReceiverClass = MetaClassPtrAlias;
102148e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    } else {
102248e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      if (!ClassPtrAlias) {
102348e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner        ClassPtrAlias = new llvm::GlobalAlias(IdTy,
102448e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
102548e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner            Class->getNameAsString(), NULL, &TheModule);
102648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      }
102748e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      ReceiverClass = ClassPtrAlias;
10285efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    }
102971238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  }
10305efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Cast the pointer to a simplified version of the class structure
1031db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateBitCast(ReceiverClass,
103296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::PointerType::getUnqual(
10337650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner        llvm::StructType::get(IdTy, IdTy, NULL)));
10345efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Get the superclass pointer
1035db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1);
10365efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Load the superclass pointer
1037db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateLoad(ReceiverClass);
103820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Construct the structure used to look up the IMP
10397650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCSuperTy = llvm::StructType::get(
104047a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson      Receiver->getType(), IdTy, NULL);
1041db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
1042b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1043db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
1044db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
104520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1046c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy);
10472acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *impType =
1048c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
1049db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
1050c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // Get the IMP
1051c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd);
1052c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  imp = EnforceType(Builder, imp, llvm::PointerType::getUnqual(impType));
105320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1054dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall  llvm::Value *impMD[] = {
1055dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::MDString::get(VMContext, Sel.getAsString()),
1056dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
1057dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsClassMessage)
1058dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall   };
10596f141659cab11109d9931d92d0988f8850778de3Jay Foad  llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
1060dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall
10614b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  llvm::Instruction *call;
1062ef072fd2f3347cfd857d6eb787b245b950771430John McCall  RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs,
10634b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall      0, &call);
10644b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  call->setMetadata(msgSendMDKind, node);
10654b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  return msgRet;
106620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
106720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
10681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Generate code for a message send expression.
10699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallRValue
10709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
1071ef072fd2f3347cfd857d6eb787b245b950771430John McCall                               ReturnValueSlot Return,
10727f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                               QualType ResultType,
10737f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                               Selector Sel,
1074f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                               llvm::Value *Receiver,
1075df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                               const CallArgList &CallArgs,
1076c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                               const ObjCInterfaceDecl *Class,
1077df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                               const ObjCMethodDecl *Method) {
10780bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall  CGBuilderTy &Builder = CGF.Builder;
10790bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall
1080664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // Strip out message sends to retain / release in GC mode
1081e6a11a62d1d66d10a87d81ed697348d66d59adf7David Chisnall  if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) {
1082ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == RetainSel || Sel == AutoreleaseSel) {
10830bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall      return RValue::get(EnforceType(Builder, Receiver,
10840bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall                  CGM.getTypes().ConvertType(ResultType)));
1085ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
1086ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == ReleaseSel) {
1087ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall      return RValue::get(0);
1088ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
1089ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
1090664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1091664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // If the return type is something that goes in an integer register, the
1092664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // runtime will handle 0 returns.  For other cases, we fill in the 0 value
1093664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // ourselves.
1094664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  //
1095664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // The language spec says the result of this kind of message send is
1096664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // undefined, but lots of people seem to have forgotten to read that
1097664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // paragraph and insist on sending messages to nil that have structure
1098664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // returns.  With GCC, this generates a random return value (whatever happens
1099664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // to be on the stack / in those registers at the time) on most platforms,
1100c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // and generates an illegal instruction trap on SPARC.  With LLVM it corrupts
1101c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // the stack.
1102c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  bool isPointerSizedReturn = (ResultType->isAnyPointerType() ||
1103c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType());
1104664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1105664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  llvm::BasicBlock *startBB = 0;
1106664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  llvm::BasicBlock *messageBB = 0;
1107a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall  llvm::BasicBlock *continueBB = 0;
1108664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1109664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  if (!isPointerSizedReturn) {
1110664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    startBB = Builder.GetInsertBlock();
1111664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    messageBB = CGF.createBasicBlock("msgSend");
1112a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    continueBB = CGF.createBasicBlock("continue");
1113664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1114664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
1115664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall            llvm::Constant::getNullValue(Receiver->getType()));
1116a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    Builder.CreateCondBr(isNil, continueBB, messageBB);
1117664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    CGF.EmitBlock(messageBB);
1118664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  }
1119664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
11200f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
1121df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  llvm::Value *cmd;
1122df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  if (Method)
1123d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    cmd = GetSelector(Builder, Method);
1124df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  else
1125d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    cmd = GetSelector(Builder, Sel);
1126c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  cmd = EnforceType(Builder, cmd, SelectorTy);
1127c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  Receiver = EnforceType(Builder, Receiver, IdTy);
1128c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
1129c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Value *impMD[] = {
1130c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        llvm::MDString::get(VMContext, Sel.getAsString()),
1131c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        llvm::MDString::get(VMContext, Class ? Class->getNameAsString() :""),
1132c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), Class!=0)
1133c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall   };
11346f141659cab11109d9931d92d0988f8850778de3Jay Foad  llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
1135b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1136c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // Get the IMP to call
1137c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Value *imp = LookupIMP(CGF, Receiver, cmd, node);
1138c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
1139c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  CallArgList ActualArgs;
114004c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  ActualArgs.add(RValue::get(Receiver), ASTIdTy);
114104c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
1142f85e193739c953358c865005855253af4f68a497John McCall  ActualArgs.addFrom(CallArgs);
1143b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1144b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  CodeGenTypes &Types = CGM.getTypes();
114504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
1146264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                       FunctionType::ExtInfo());
11472acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *impType =
11486793966aa221764f579cc8436bd641e1ec339c6dDaniel Dunbar    Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
1149c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  imp = EnforceType(Builder, imp, llvm::PointerType::getUnqual(impType));
115063e742bed90aab2e8e778e48fbbd4bce766b4373David Chisnall
115163e742bed90aab2e8e778e48fbbd4bce766b4373David Chisnall
115234e65770ad6142b033d6cec4f476ed20fb248bf3Fariborz Jahanian  // For sender-aware dispatch, we pass the sender as the third argument to a
115334e65770ad6142b033d6cec4f476ed20fb248bf3Fariborz Jahanian  // lookup function.  When sending messages from C code, the sender is nil.
1154d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
11554b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  llvm::Instruction *call;
1156ef072fd2f3347cfd857d6eb787b245b950771430John McCall  RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs,
11574b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall      0, &call);
11584b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  call->setMetadata(msgSendMDKind, node);
1159664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1160a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall
1161664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  if (!isPointerSizedReturn) {
1162a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    messageBB = CGF.Builder.GetInsertBlock();
1163a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    CGF.Builder.CreateBr(continueBB);
1164a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    CGF.EmitBlock(continueBB);
1165664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    if (msgRet.isScalar()) {
1166664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      llvm::Value *v = msgRet.getScalarVal();
1167bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
1168664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v, messageBB);
1169664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
1170664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::get(phi);
1171664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    } else if (msgRet.isAggregate()) {
1172664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      llvm::Value *v = msgRet.getAggregateAddr();
1173bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
11742acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType());
1175866163bf6c66211a9b4d5ba160ac02de04ce4be6David Chisnall      llvm::AllocaInst *NullVal =
1176866163bf6c66211a9b4d5ba160ac02de04ce4be6David Chisnall          CGF.CreateTempAlloca(RetTy->getElementType(), "null");
1177664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      CGF.InitTempAlloca(NullVal,
1178664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          llvm::Constant::getNullValue(RetTy->getElementType()));
1179664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v, messageBB);
1180664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(NullVal, startBB);
1181664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::getAggregate(phi);
1182664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    } else /* isComplex() */ {
1183664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
1184bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
1185664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v.first, messageBB);
1186664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
1187664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          startBB);
1188bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
1189664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi2->addIncoming(v.second, messageBB);
1190664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
1191664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          startBB);
1192664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::getComplex(phi, phi2);
1193664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    }
1194664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  }
1195664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  return msgRet;
119620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
119720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Generates a MethodList.  Used in construction of a objc_class and
119920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// objc_category structures.
12005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerllvm::Constant *CGObjCGNU::GenerateMethodList(const StringRef &ClassName,
12015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                              const StringRef &CategoryName,
12025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const SmallVectorImpl<Selector> &MethodSels,
12035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const SmallVectorImpl<llvm::Constant *> &MethodTypes,
120420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    bool isClassMethodList) {
12050f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  if (MethodSels.empty())
12060f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    return NULLPtr;
12071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
12087650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCMethodTy = llvm::StructType::get(
120920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Really a selector, but the runtime creates it us.
121020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Method types
1211c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    IMPTy, //Method pointer
121220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    NULL);
121320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Methods;
121420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
121520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
121620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
12179f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *Method =
121820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
12199f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                                MethodSels[i],
12209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                                isClassMethodList));
12219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    assert(Method && "Can't generate metadata for method that doesn't exist");
12229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
12239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(C);
12249f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(MethodTypes[i]);
12259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Method = llvm::ConstantExpr::getBitCast(Method,
1226c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        IMPTy);
12279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(Method);
12289f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
122920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
123020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
123120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of method structures
123296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
12331e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian                                                            Methods.size());
12347db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
1235fba676396703e87a5034e5f308aa30a633468f66Chris Lattner                                                         Methods);
123620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
123720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Structure containing list pointer, array and array count
12389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *ObjCMethodListTy =
12399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::StructType::createNamed(VMContext, "");
12409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(ObjCMethodListTy);
12419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  ObjCMethodListTy->setBody(
12421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NextPtrTy,
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      IntTy,
124420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ObjCMethodArrayTy,
124520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      NULL);
124620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
124720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.clear();
124803e205031b08669f05c41eed5b896fc94c4a12bbOwen Anderson  Methods.push_back(llvm::ConstantPointerNull::get(
124996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson        llvm::PointerType::getUnqual(ObjCMethodListTy)));
12500032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
125120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        MethodTypes.size()));
125220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.push_back(MethodArray);
12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
125420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
125520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
125620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
125720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
125820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generates an IvarList.  Used in construction of a objc_class.
125920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateIvarList(
12605f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const SmallVectorImpl<llvm::Constant *>  &IvarNames,
12615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const SmallVectorImpl<llvm::Constant *>  &IvarTypes,
12625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const SmallVectorImpl<llvm::Constant *>  &IvarOffsets) {
12631804463ee790ac3cc73f72597ccb38b983f7d263David Chisnall  if (IvarNames.size() == 0)
12641804463ee790ac3cc73f72597ccb38b983f7d263David Chisnall    return NULLPtr;
12651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
12667650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCIvarTy = llvm::StructType::get(
126720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
126820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
126920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    IntTy,
127020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    NULL);
127120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Ivars;
127220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
127320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
127420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
12758a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(IvarNames[i]);
12768a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(IvarTypes[i]);
127720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.push_back(IvarOffsets[i]);
127808e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson    Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
127920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
128020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
128120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of method structures
128296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
128320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IvarNames.size());
128420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
12851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
12874a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
12887db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
128920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Structure containing array and array count
12907650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
129120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    ObjCIvarArrayTy,
129220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    NULL);
129320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
129420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
129520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
12960f984268b05edab2cc555a427c441baa9c252658Chris Lattner}
12970f984268b05edab2cc555a427c441baa9c252658Chris Lattner
129820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generate a class structure
129920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateClassStructure(
130020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *MetaClass,
130120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *SuperClass,
130220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    unsigned info,
1303d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner    const char *Name,
130420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Version,
130520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *InstanceSize,
130620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *IVars,
130720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Methods,
1308d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *Protocols,
1309d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *IvarOffsets,
13108c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall    llvm::Constant *Properties,
13118c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall    bool isMeta) {
131220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Set up the class structure
131320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Note:  Several of these are char*s when they should be ids.  This is
131420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // because the runtime performs this translation on load.
1315d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  //
1316d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Fields marked New ABI are part of the GNUstep runtime.  We emit them
1317d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // anyway; the classes will still work with the GNU runtime, they will just
1318d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // be ignored.
13197650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ClassTy = llvm::StructType::get(
132020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,        // class_pointer
132120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,        // super_class
132220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,        // name
132320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // version
132420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // info
132520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // instance_size
132620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IVars->getType(),   // ivars
132720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Methods->getType(), // methods
13281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // These are all filled in by the runtime, so we pretend
132920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // dtable
133020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // subclass_list
133120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // sibling_class
133220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // protocols
133320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // gc_object_type
1334d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      // New ABI:
1335d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      LongTy,                 // abi_version
1336d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      IvarOffsets->getType(), // ivar_offsets
1337d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Properties->getType(),  // properties
133820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      NULL);
13394a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
134020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Fill in the structure
134120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
13423c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
134320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(SuperClass);
1344d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner  Elements.push_back(MakeConstantString(Name, ".class_name"));
134520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(Zero);
13464a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(LongTy, info));
134705f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall  if (isMeta) {
134805f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall    llvm::TargetData td(&TheModule);
1349e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck    Elements.push_back(
1350e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck        llvm::ConstantInt::get(LongTy,
1351e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck                               td.getTypeSizeInBits(ClassTy) /
1352e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck                                 CGM.getContext().getCharWidth()));
135305f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall  } else
135405f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall    Elements.push_back(InstanceSize);
135520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(IVars);
135620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(Methods);
1357d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1358d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1359d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
13603c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
1361d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1362d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(Zero);
1363d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(IvarOffsets);
1364d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(Properties);
136520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
136641d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // This is now an externally visible symbol, so that we can speed up class
136741d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // messages in the next ABI.
13688c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall  return MakeGlobal(ClassTy, Elements, (isMeta ? "_OBJC_METACLASS_":
13698c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall      "_OBJC_CLASS_") + std::string(Name), llvm::GlobalValue::ExternalLinkage);
137020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
137120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
137220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
13735f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const SmallVectorImpl<llvm::Constant *>  &MethodNames,
13745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const SmallVectorImpl<llvm::Constant *>  &MethodTypes) {
13751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
13767650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
137720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
137820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
137920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    NULL);
138020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Methods;
138120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
138220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
138320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
13841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Elements.push_back(MethodNames[i]);
13858a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(MethodTypes[i]);
138608e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson    Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
138720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
138896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
138920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      MethodNames.size());
13907db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
1391bb1c8600218b3244340331165fc7cba7bf227655Mike Stump                                                   Methods);
13927650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
139320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IntTy, ObjCMethodArrayTy, NULL);
139420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.clear();
13954a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
139620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.push_back(Array);
139720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
139820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
1399bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
140020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov// Create the protocol list structure used in classes, categories and so on
140120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateProtocolList(
14025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const SmallVectorImpl<std::string> &Protocols) {
140396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
140420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Protocols.size());
14057650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ProtocolListTy = llvm::StructType::get(
140620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy, //Should be a recurisve pointer, but it's always NULL here.
14078fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall      SizeTy,
140820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ProtocolArrayTy,
140920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      NULL);
14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
141120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
141220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      iter != endIter ; iter++) {
1413ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    llvm::Constant *protocol = 0;
1414ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    llvm::StringMap<llvm::Constant*>::iterator value =
1415ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall      ExistingProtocols.find(*iter);
1416ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    if (value == ExistingProtocols.end()) {
1417f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      protocol = GenerateEmptyProtocol(*iter);
1418ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    } else {
1419ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall      protocol = value->getValue();
1420ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    }
14213c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
1422a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson                                                           PtrToInt8Ty);
142320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.push_back(Ptr);
142420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
14257db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
142620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Elements);
142720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
142820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
14294a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
143020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ProtocolArray);
143120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
143220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
143320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
1435af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar                                            const ObjCProtocolDecl *PD) {
1436f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
14372acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *T =
1438f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian    CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
143996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
1440f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian}
1441f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian
1442f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanianllvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
1443f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  const std::string &ProtocolName) {
14445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::string, 0> EmptyStringVector;
14455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 0> EmptyConstantVector;
1446f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian
1447f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
1448d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *MethodList =
1449f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian    GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
1450f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // Protocols are objects containing lists of the methods implemented and
1451f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // protocols adopted.
14527650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
1453f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      PtrToInt8Ty,
1454f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      ProtocolList->getType(),
1455d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1456d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1457d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1458d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1459f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      NULL);
14601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
1461f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // The isa pointer must be set to a magic number so the runtime knows it's
1462f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // the correct layout.
14633c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getIntToPtr(
14649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
14659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall          ProtocolVersion), IdTy));
1466f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1467f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  Elements.push_back(ProtocolList);
1468d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1469d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1470d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1471d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1472f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
147320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
147420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1475af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbarvoid CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
1476af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  ASTContext &Context = CGM.getContext();
14778ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ProtocolName = PD->getNameAsString();
14785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::string, 16> Protocols;
1479af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
1480af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar       E = PD->protocol_end(); PI != E; ++PI)
1481d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner    Protocols.push_back((*PI)->getNameAsString());
14825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> InstanceMethodNames;
14835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
14845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
14855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
148617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
148717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->instmeth_end(); iter != E; iter++) {
1488af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    std::string TypeStr;
1489af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
1490d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1491d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodNames.push_back(
1492d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          MakeConstantString((*iter)->getSelector().getAsString()));
1493d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1494d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1495d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodNames.push_back(
1496d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          MakeConstantString((*iter)->getSelector().getAsString()));
1497d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1498d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1499af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  }
1500af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  // Collect information about class methods:
15015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> ClassMethodNames;
15025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
15035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
15045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
15051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (ObjCProtocolDecl::classmeth_iterator
150617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
150717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       iter != endIter ; iter++) {
1508af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    std::string TypeStr;
1509af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
1510d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1511d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ClassMethodNames.push_back(
1512d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          MakeConstantString((*iter)->getSelector().getAsString()));
1513d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ClassMethodTypes.push_back(MakeConstantString(TypeStr));
1514d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1515d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodNames.push_back(
1516d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          MakeConstantString((*iter)->getSelector().getAsString()));
1517d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
1518d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1519af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  }
152020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
152120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
152220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *InstanceMethodList =
152320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
152420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassMethodList =
152520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
1526d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalInstanceMethodList =
1527d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateProtocolMethodList(OptionalInstanceMethodNames,
1528d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            OptionalInstanceMethodTypes);
1529d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalClassMethodList =
1530d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateProtocolMethodList(OptionalClassMethodNames,
1531d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            OptionalClassMethodTypes);
1532d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1533d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Property metadata: name, attributes, isSynthesized, setter name, setter
1534d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // types, getter name, getter types.
1535d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // The isSynthesized value is always set to 0 in a protocol.  It exists to
1536d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // simplify the runtime library by allowing it to use the same data
1537d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // structures for protocol metadata everywhere.
15387650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
1539d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1540d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          PtrToInt8Ty, NULL);
1541d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Properties;
1542d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> OptionalProperties;
1543d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1544d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all of the property methods need adding to the method list and to the
1545d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // property metadata list.
1546d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  for (ObjCContainerDecl::prop_iterator
1547d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian         iter = PD->prop_begin(), endIter = PD->prop_end();
1548d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       iter != endIter ; iter++) {
1549d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    std::vector<llvm::Constant*> Fields;
1550d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    ObjCPropertyDecl *property = (*iter);
1551d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1552d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(MakeConstantString(property->getNameAsString()));
1553d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1554d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian                property->getPropertyAttributes()));
1555d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
1556d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1557d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1558d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1559d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1560d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodTypes.push_back(TypeEncoding);
1561d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1562d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1563d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1564d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1565d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1566d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1567d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1568d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1569d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1570d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1571d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodTypes.push_back(TypeEncoding);
1572d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1573d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1574d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1575d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1576d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1577d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1578d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1579d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1580d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1581d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1582d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1583d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
1584d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1585d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1586d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* PropertyListInitFields[] =
1587d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1588d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1589d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyListInit =
1590c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner      llvm::ConstantStruct::getAnon(PropertyListInitFields);
1591d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1592d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1593d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyListInit, ".objc_property_list");
1594d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1595d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyArray =
1596d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1597d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          OptionalProperties.size()) , OptionalProperties);
1598d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* OptionalPropertyListInitFields[] = {
1599d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1600d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalPropertyArray };
1601d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1602d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyListInit =
1603c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner      llvm::ConstantStruct::getAnon(OptionalPropertyListInitFields);
1604d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1605d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          OptionalPropertyListInit->getType(), false,
1606d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1607d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ".objc_property_list");
1608d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
160920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Protocols are objects containing lists of the methods implemented and
161020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // protocols adopted.
16117650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
161220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,
161320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ProtocolList->getType(),
161420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      InstanceMethodList->getType(),
161520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ClassMethodList->getType(),
1616d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodList->getType(),
1617d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodList->getType(),
1618d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyList->getType(),
1619d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalPropertyList->getType(),
162020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      NULL);
16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
162220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // The isa pointer must be set to a magic number so the runtime knows it's
162320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // the correct layout.
16243c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getIntToPtr(
16259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
16269f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall          ProtocolVersion), IdTy));
162720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
162820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ProtocolList);
162920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(InstanceMethodList);
163020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ClassMethodList);
1631d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalInstanceMethodList);
1632d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalClassMethodList);
1633d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(PropertyList);
1634d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalPropertyList);
16351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ExistingProtocols[ProtocolName] =
16363c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
163720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          ".objc_protocol"), IdTy);
163820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
1639d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanianvoid CGObjCGNU::GenerateProtocolHolderCategory(void) {
1640d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Collect information about instance methods
16415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 1> MethodSels;
16425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 1> MethodTypes;
1643d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1644d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Elements;
1645d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1646d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  const std::string CategoryName = "AnotherHack";
1647d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MakeConstantString(CategoryName));
1648d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MakeConstantString(ClassName));
1649d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Instance method list
1650d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1651d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1652d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Class method list
1653d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1654d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1655d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Protocol list
1656d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1657d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ExistingProtocols.size());
16587650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ProtocolListTy = llvm::StructType::get(
1659d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PtrTy, //Should be a recurisve pointer, but it's always NULL here.
16608fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall      SizeTy,
1661d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ProtocolArrayTy,
1662d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      NULL);
1663d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> ProtocolElements;
1664d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  for (llvm::StringMapIterator<llvm::Constant*> iter =
1665d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1666d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       iter != endIter ; iter++) {
1667d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1668d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            PtrTy);
1669d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    ProtocolElements.push_back(Ptr);
1670d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
1671d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1672d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ProtocolElements);
1673d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.clear();
1674d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(NULLPtr);
1675d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1676d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian              ExistingProtocols.size()));
1677d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(ProtocolArray);
1678d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1679d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian                  ProtocolElements, ".objc_protocol_list"), PtrTy));
1680d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Categories.push_back(llvm::ConstantExpr::getBitCast(
16817650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner        MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
1682d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
1683d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian}
168420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
16857ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbarvoid CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
16868ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ClassName = OCD->getClassInterface()->getNameAsString();
16878ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string CategoryName = OCD->getNameAsString();
16887ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance methods
16895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 16> InstanceMethodSels;
16905f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
1691653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor  for (ObjCCategoryImplDecl::instmeth_iterator
169217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
1693653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor       iter != endIter ; iter++) {
16947ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    InstanceMethodSels.push_back((*iter)->getSelector());
16957ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
16967ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
16978a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
16987ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
16997ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
17007ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about class methods
17015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 16> ClassMethodSels;
17025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
17031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (ObjCCategoryImplDecl::classmeth_iterator
170417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
1705653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor       iter != endIter ; iter++) {
17067ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    ClassMethodSels.push_back((*iter)->getSelector());
17077ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
17087ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
17098a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    ClassMethodTypes.push_back(MakeConstantString(TypeStr));
17107ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
17117ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
17127ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect the names of referenced protocols
17135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::string, 16> Protocols;
1714ad9e06d82ae627e858e5bfe4a7f1f9337b5b0a15David Chisnall  const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
1715ad9e06d82ae627e858e5bfe4a7f1f9337b5b0a15David Chisnall  const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
17167ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
17177ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar       E = Protos.end(); I != E; ++I)
1718d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner    Protocols.push_back((*I)->getNameAsString());
17197ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
172020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
172120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(CategoryName));
172220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(ClassName));
17231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Instance method list
17243c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1725a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner          ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
172620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          false), PtrTy));
172720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Class method list
17283c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1729a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner          ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
173020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        PtrTy));
173120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Protocol list
17323c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(
173320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        GenerateProtocolList(Protocols), PtrTy));
17343c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Categories.push_back(llvm::ConstantExpr::getBitCast(
17357650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner        MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
173647a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson            PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
173720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
17387ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
1739d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanianllvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
17405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVectorImpl<Selector> &InstanceMethodSels,
17415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
1742d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ASTContext &Context = CGM.getContext();
1743d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  //
1744d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Property metadata: name, attributes, isSynthesized, setter name, setter
1745d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // types, getter name, getter types.
17467650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
1747d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1748d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          PtrToInt8Ty, NULL);
1749d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Properties;
1750d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1751d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1752d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all of the property methods need adding to the method list and to the
1753d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // property metadata list.
1754d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  for (ObjCImplDecl::propimpl_iterator
1755d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian         iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
1756d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       iter != endIter ; iter++) {
1757d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    std::vector<llvm::Constant*> Fields;
1758d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
175942ba04a072c69d84a6660e4498215086662910acDavid Chisnall    ObjCPropertyImplDecl *propertyImpl = *iter;
176042ba04a072c69d84a6660e4498215086662910acDavid Chisnall    bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
176142ba04a072c69d84a6660e4498215086662910acDavid Chisnall        ObjCPropertyImplDecl::Synthesize);
1762d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1763d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(MakeConstantString(property->getNameAsString()));
1764d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1765d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian                property->getPropertyAttributes()));
176642ba04a072c69d84a6660e4498215086662910acDavid Chisnall    Fields.push_back(llvm::ConstantInt::get(Int8Ty, isSynthesized));
1767d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1768d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1769d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1770d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
177142ba04a072c69d84a6660e4498215086662910acDavid Chisnall      if (isSynthesized) {
177242ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodTypes.push_back(TypeEncoding);
177342ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodSels.push_back(getter->getSelector());
177442ba04a072c69d84a6660e4498215086662910acDavid Chisnall      }
1775d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1776d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1777d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1778d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1779d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1780d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1781d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1782d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1783d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1784d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
178542ba04a072c69d84a6660e4498215086662910acDavid Chisnall      if (isSynthesized) {
178642ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodTypes.push_back(TypeEncoding);
178742ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodSels.push_back(setter->getSelector());
178842ba04a072c69d84a6660e4498215086662910acDavid Chisnall      }
1789d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1790d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1791d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1792d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1793d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1794d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1795d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1796d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
1797d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::ArrayType *PropertyArrayTy =
1798d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
1799d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
1800d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          Properties);
1801d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* PropertyListInitFields[] =
1802d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1803d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1804d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyListInit =
1805c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner      llvm::ConstantStruct::getAnon(PropertyListInitFields);
1806d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
1807d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          llvm::GlobalValue::InternalLinkage, PropertyListInit,
1808d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ".objc_property_list");
1809d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian}
1810d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
18117ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbarvoid CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
18127ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  ASTContext &Context = CGM.getContext();
18137ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
18147ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Get the superclass name.
18151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ObjCInterfaceDecl * SuperClassDecl =
18167ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    OID->getClassInterface()->getSuperClass();
18178ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string SuperClassName;
18182a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  if (SuperClassDecl) {
18198ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner    SuperClassName = SuperClassDecl->getNameAsString();
18202a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner    EmitClassRef(SuperClassName);
18212a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  }
18227ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
18237ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Get the class name
182409dc6660487fc2f4a4bb2032e30123d3e0da7230Chris Lattner  ObjCInterfaceDecl *ClassDecl =
182509dc6660487fc2f4a4bb2032e30123d3e0da7230Chris Lattner    const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
18268ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ClassName = ClassDecl->getNameAsString();
18272a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // Emit the symbol that is used to generate linker errors if this class is
18282a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // referenced in other modules but not declared.
1829c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  std::string classSymbolName = "__objc_class_name_" + ClassName;
18301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::GlobalVariable *symbol =
1831c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian      TheModule.getGlobalVariable(classSymbolName)) {
18324a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson    symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
1833c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  } else {
18341c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    new llvm::GlobalVariable(TheModule, LongTy, false,
18354a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson    llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
18361c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    classSymbolName);
1837c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  }
18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18392bebbf0acee55404de4b8846713b64429e744e8fDaniel Dunbar  // Get the size of instances.
18405f022d82696c84e4d127c558871d68ac6273274eKen Dyck  int instanceSize =
18415f022d82696c84e4d127c558871d68ac6273274eKen Dyck    Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
18427ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
18437ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance variables.
18445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> IvarNames;
18455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> IvarTypes;
18465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> IvarOffsets;
18471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1848d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> IvarOffsetValues;
1849d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
18501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  int superInstanceSize = !SuperClassDecl ? 0 :
18515f022d82696c84e4d127c558871d68ac6273274eKen Dyck    Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
18529cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // For non-fragile ivars, set the instance size to 0 - {the size of just this
18539cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // class}.  The runtime will then set this to the correct value on load.
18549cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
18559cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    instanceSize = 0 - (instanceSize - superInstanceSize);
18569cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
18577f63cb098f2461643f71ea590c1a477603f27f63David Chisnall
1858db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
1859db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose       IVD = IVD->getNextIvar()) {
18607ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Store the name
18617f63cb098f2461643f71ea590c1a477603f27f63David Chisnall      IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
18627ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Get the type encoding for this ivar
18637ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      std::string TypeStr;
18647f63cb098f2461643f71ea590c1a477603f27f63David Chisnall      Context.getObjCEncodingForType(IVD->getType(), TypeStr);
18658a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarTypes.push_back(MakeConstantString(TypeStr));
18667ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Get the offset
1867d901da531433254210a08e8b1f9e1ad049b340aaDavid Chisnall      uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
1868aecbf24a6bad049437f8fec97e557d414003a3dbDavid Chisnall      uint64_t Offset = BaseOffset;
18699cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
1870d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian        Offset = BaseOffset - superInstanceSize;
18719cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      }
187263ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
187363ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      // Create the direct offset value
187463ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." +
187563ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          IVD->getNameAsString();
187663ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName);
187763ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      if (OffsetVar) {
187863ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        OffsetVar->setInitializer(OffsetValue);
187963ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        // If this is the real definition, change its linkage type so that
188063ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        // different modules will use this one, rather than their private
188163ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        // copy.
188263ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage);
188363ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      } else
188463ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        OffsetVar = new llvm::GlobalVariable(TheModule, IntTy,
1885d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          false, llvm::GlobalValue::ExternalLinkage,
188663ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          OffsetValue,
1887d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          "__objc_ivar_offset_value_" + ClassName +"." +
188863ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          IVD->getNameAsString());
188963ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      IvarOffsets.push_back(OffsetValue);
189063ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      IvarOffsetValues.push_back(OffsetVar);
18917ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
18929f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::GlobalVariable *IvarOffsetArray =
18939f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    MakeGlobalArray(PtrToIntTy, IvarOffsetValues, ".ivar.offsets");
18949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
18957ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
18967ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance methods
18975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 16> InstanceMethodSels;
18985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
18991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (ObjCImplementationDecl::instmeth_iterator
190017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
1901653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor       iter != endIter ; iter++) {
19027ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    InstanceMethodSels.push_back((*iter)->getSelector());
19037ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
19047ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
19058a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
19067ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
1907d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1908d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
1909d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          InstanceMethodTypes);
1910d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
19117ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
19127ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about class methods
19135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 16> ClassMethodSels;
19145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
1915653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor  for (ObjCImplementationDecl::classmeth_iterator
191617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
1917653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor       iter != endIter ; iter++) {
19187ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    ClassMethodSels.push_back((*iter)->getSelector());
19197ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
19207ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
19218a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    ClassMethodTypes.push_back(MakeConstantString(TypeStr));
19227ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
19237ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect the names of referenced protocols
19245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::string, 16> Protocols;
19257ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
19267ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
19277ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar       E = Protos.end(); I != E; ++I)
1928d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner    Protocols.push_back((*I)->getNameAsString());
19297ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
19307ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
19317ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
193220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Get the superclass pointer.
193320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *SuperClass;
19348ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  if (!SuperClassName.empty()) {
193520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
193620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  } else {
193703e205031b08669f05c41eed5b896fc94c4a12bbOwen Anderson    SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
193820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
193920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Empty vector used to construct empty method lists
19405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 1>  empty;
194120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate the method and instance variable lists
194220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
1943a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner      InstanceMethodSels, InstanceMethodTypes, false);
194420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
1945a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner      ClassMethodSels, ClassMethodTypes, true);
194620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
194720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IvarOffsets);
19481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
19498a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // we emit a symbol containing the offset for each ivar in the class.  This
19508a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // allows code compiled for the non-Fragile ABI to inherit from code compiled
19518a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // for the legacy ABI, without causing problems.  The converse is also
19528a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // possible, but causes all ivar accesses to be fragile.
1953e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall
19548a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // Offset pointer for getting at the correct field in the ivar list when
19558a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // setting up the alias.  These are: The base address for the global, the
19568a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // ivar array (second field), the ivar in this list (set for each ivar), and
19578a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // the offset (third field in ivar structure)
19582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext);
19598a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
19601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      llvm::ConstantInt::get(IndexTy, 1), 0,
19618a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::ConstantInt::get(IndexTy, 2) };
19628a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall
1963db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  unsigned ivarIndex = 0;
1964db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
1965db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose       IVD = IVD->getNextIvar()) {
19668a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
1967e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall          + IVD->getNameAsString();
1968db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex);
19698a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      // Get the correct ivar field
19708a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
1971a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad              IvarList, offsetPointerIndexes);
1972e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall      // Get the existing variable, if one exists.
19738a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
19748a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      if (offset) {
19758a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          offset->setInitializer(offsetValue);
19768a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          // If this is the real definition, change its linkage type so that
19778a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          // different modules will use this one, rather than their private
19788a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          // copy.
19798a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
19808a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      } else {
19818a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          // Add a new alias if there isn't one already.
19828a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall          offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
19838a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall                  false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
19848a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      }
1985db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      ++ivarIndex;
19868a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  }
198720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  //Generate metaclass for class methods
198820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
19891804463ee790ac3cc73f72597ccb38b983f7d263David Chisnall      NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList(
19908c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall        empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr, true);
19915efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
199220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate the class structure
19938ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  llvm::Constant *ClassStruct =
1994d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
19958ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner                           ClassName.c_str(), 0,
19964a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
1997d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
1998d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Properties);
19995efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
20005efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Resolve the class aliases, if they exist.
20015efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  if (ClassPtrAlias) {
20020b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    ClassPtrAlias->replaceAllUsesWith(
20033c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson        llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
20040b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    ClassPtrAlias->eraseFromParent();
20055efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    ClassPtrAlias = 0;
20065efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  }
20075efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  if (MetaClassPtrAlias) {
20080b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    MetaClassPtrAlias->replaceAllUsesWith(
20093c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson        llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
20100b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    MetaClassPtrAlias->eraseFromParent();
20115efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    MetaClassPtrAlias = 0;
20125efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  }
20135efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
201420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Add class structure to list to be added to the symtab later
20153c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
201620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(ClassStruct);
201720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
201820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
2019c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian
20201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function *CGObjCGNU::ModuleInitFunction() {
202120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Only emit an ObjC load function if no Objective-C stuff has been called
202220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
20239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      ExistingProtocols.empty() && SelectorTable.empty())
202420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    return NULL;
20251b8956e1ce4c5770982d6e59edb16979b500677aEli Friedman
2026d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all referenced protocols to a category.
2027d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  GenerateProtocolHolderCategory();
2028d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
20292acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
2030e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner          SelectorTy->getElementType());
2031ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type *SelStructPtrTy = SelectorTy;
2032e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  if (SelStructTy == 0) {
20337650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
203496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
2035e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  }
2036e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
203720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
203871238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  llvm::Constant *Statics = NULLPtr;
203920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate statics list:
204071238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  if (ConstantStrings.size()) {
204196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
204271238f67c5df13eef19450d66b7a7ab28377192aChris Lattner        ConstantStrings.size() + 1);
204371238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    ConstantStrings.push_back(NULLPtr);
20448a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall
20455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass;
20469f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20471b0969590e712d7d52fc9c0d43d3ab85c36d07a6Daniel Dunbar    if (StringClass.empty()) StringClass = "NXConstantString";
20489f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20498a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(MakeConstantString(StringClass,
20508a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall                ".objc_static_class_name"));
20517db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson    Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
205271238f67c5df13eef19450d66b7a7ab28377192aChris Lattner       ConstantStrings));
20531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::StructType *StaticsListTy =
20547650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner      llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
2055a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson    llvm::Type *StaticsListPtrTy =
205696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::PointerType::getUnqual(StaticsListTy);
205771238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
20581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::ArrayType *StaticsListArrayTy =
205996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::ArrayType::get(StaticsListPtrTy, 2);
206071238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Elements.clear();
206171238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Elements.push_back(Statics);
2062c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson    Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
206371238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
20643c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
206571238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  }
206620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of classes, categories, and constant objects
206796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
206820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Classes.size() + Categories.size()  + 2);
20697650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
20700032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                     llvm::Type::getInt16Ty(VMContext),
20710032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                     llvm::Type::getInt16Ty(VMContext),
2072630404b16e886238ed68a1abbfe2e548f43aa664Chris Lattner                                                     ClassListTy, NULL);
207320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
207420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
207520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Pointer to an array of selectors used in this module.
207620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Selectors;
20779f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::vector<llvm::GlobalAlias*> SelectorAliases;
20789f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  for (SelectorMap::iterator iter = SelectorTable.begin(),
20799f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {
20809f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    std::string SelNameStr = iter->first.getAsString();
20829f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");
20839f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVectorImpl<TypedSelector> &Types = iter->second;
20855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
20869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        e = Types.end() ; i!=e ; i++) {
20879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20889f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      llvm::Constant *SelectorTypeEncoding = NULLPtr;
20899f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      if (!i->first.empty())
20909f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");
20919f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20929f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.push_back(SelName);
20939f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.push_back(SelectorTypeEncoding);
20949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
20959f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.clear();
20969f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
20979f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      // Store the selector alias for later replacement
20989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      SelectorAliases.push_back(i->second);
20999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    }
210020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
21019f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  unsigned SelectorCount = Selectors.size();
21029f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // NULL-terminate the selector list.  This should not actually be required,
21039f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // because the selector list has a length field.  Unfortunately, the GCC
21049f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // runtime decides to ignore the length field and expects a NULL terminator,
21059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // and GCC cooperates with this by always setting the length to 0.
210620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
210720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
210808e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson  Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
210920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
21109f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
211120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of static selectors
21129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount));
21139f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::Constant *SelectorList = MakeGlobalArray(SelStructTy, Selectors,
211420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          ".objc_selector_list");
21151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
2116e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    SelStructPtrTy));
211720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
211820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Now that all of the static selectors exist, create pointers to them.
21199f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  for (unsigned int i=0 ; i<SelectorCount ; i++) {
21209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
212120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Idxs[] = {Zeros[0],
21229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), i), Zeros[0]};
21239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    // FIXME: We're generating redundant loads and stores here!
2124c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(SelectorList,
2125a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad        makeArrayRef(Idxs, 2));
2126e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    // If selectors are defined as an opaque type, cast the pointer to this
2127e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    // type.
2128c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
21299f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorAliases[i]->replaceAllUsesWith(SelPtr);
21309f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorAliases[i]->eraseFromParent();
213120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
21329f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
213320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of classes defined.
21341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
213520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        Classes.size()));
213620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of categories defined
21371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
213820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        Categories.size()));
213920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an array of classes, then categories, then static object instances
214020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.insert(Classes.end(), Categories.begin(), Categories.end());
214120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  //  NULL-terminated list of static object instances (mainly constant strings)
214220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(Statics);
214320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(NULLPtr);
21447db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
214520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ClassList);
21461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Construct the symbol table
214720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
214820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
214920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // The symbol table is contained in a module which has some version-checking
215020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // constants
21517650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
2152a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall      PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy),
2153f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall      (RuntimeVersion >= 10) ? IntTy : NULL, NULL);
215420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
21559f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // Runtime version, used for ABI compatibility checking.
21569f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
215791a0b51438e5259a79d68d7450c453827beed9e2Fariborz Jahanian  // sizeof(ModuleTy)
215874a8bbf0654fbbf37be04b240b5bbe591f37f242Benjamin Kramer  llvm::TargetData td(&TheModule);
2159e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck  Elements.push_back(
2160e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck    llvm::ConstantInt::get(LongTy,
2161e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck                           td.getTypeSizeInBits(ModuleTy) /
2162e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck                             CGM.getContext().getCharWidth()));
21639f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
21649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // The path to the source file where this module was declared
21659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SourceManager &SM = CGM.getContext().getSourceManager();
21669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
21679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::string path =
21689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
21699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
217020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(SymTab);
2171a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall
2172f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall  if (RuntimeVersion >= 10)
2173f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall    switch (CGM.getLangOptions().getGCMode()) {
2174f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall      case LangOptions::GCOnly:
2175a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall        Elements.push_back(llvm::ConstantInt::get(IntTy, 2));
2176a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall        break;
2177f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall      case LangOptions::NonGC:
2178f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall        if (CGM.getLangOptions().ObjCAutoRefCount)
2179f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall          Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2180f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall        else
2181f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall          Elements.push_back(llvm::ConstantInt::get(IntTy, 0));
2182f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall        break;
2183f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall      case LangOptions::HybridGC:
2184f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall          Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2185f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall        break;
2186f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall    }
2187a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall
218820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
218920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
219020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create the load function calling the runtime entry point with the module
219120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // structure
219220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Function * LoadFunction = llvm::Function::Create(
21930032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
219420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::GlobalValue::InternalLinkage, ".objc_load_function",
219520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      &TheModule);
21960032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::BasicBlock *EntryBB =
21970032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
2198a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson  CGBuilderTy Builder(VMContext);
219920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.SetInsertPoint(EntryBB);
220026c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian
220195d318c4c10437db40ca6e15fdf32e04012da58eBenjamin Kramer  llvm::FunctionType *FT =
2202da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    llvm::FunctionType::get(Builder.getVoidTy(),
2203da549e8995c447542d5631b8b67fcc3a9582797aJay Foad                            llvm::PointerType::getUnqual(ModuleTy), true);
220495d318c4c10437db40ca6e15fdf32e04012da58eBenjamin Kramer  llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
220520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.CreateCall(Register, Module);
220620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.CreateRetVoid();
22079cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian
220820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return LoadFunction;
220920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
22107ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
2211679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianllvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
22121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const ObjCContainerDecl *CD) {
22131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ObjCCategoryImplDecl *OCD =
22143e0a540b6d846178857289ec0eb8470a278d11a3Steve Naroff    dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
22155f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef CategoryName = OCD ? OCD->getName() : "";
22165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef ClassName = CD->getName();
22179f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Selector MethodName = OMD->getSelector();
2218f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  bool isClassMethod = !OMD->isInstanceMethod();
2219391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
2220541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  CodeGenTypes &Types = CGM.getTypes();
22212acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *MethodTy =
2222541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
222320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
222420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      MethodName, isClassMethod);
222520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
2226d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar  llvm::Function *Method
22271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = llvm::Function::Create(MethodTy,
22281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             llvm::GlobalValue::InternalLinkage,
22291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             FunctionName,
22301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             &TheModule);
2231391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  return Method;
2232391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner}
2233391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
2234789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnallllvm::Constant *CGObjCGNU::GetPropertyGetFunction() {
22359f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetPropertyFn;
223649f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar}
223749f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar
2238789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnallllvm::Constant *CGObjCGNU::GetPropertySetFunction() {
22399f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return SetPropertyFn;
224049f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar}
224149f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar
2242789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnallllvm::Constant *CGObjCGNU::GetGetStructFunction() {
22439f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetStructPropertyFn;
22448fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall}
2245789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnallllvm::Constant *CGObjCGNU::GetSetStructFunction() {
22469f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return SetStructPropertyFn;
22476cc590602f41c3e98e8af0023d54296c8eca7910Fariborz Jahanian}
22486cc590602f41c3e98e8af0023d54296c8eca7910Fariborz Jahanian
2249309a4368cd299cff30ab22709e2b772cf8059e45Daniel Dunbarllvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
22509f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return EnumerationMutationFn;
22512abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson}
22522abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson
22539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
2254f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                     const ObjCAtSynchronizedStmt &S) {
22559735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
2256f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
22575dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
22585dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
22599f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
2260f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                            const ObjCAtTryStmt &S) {
2261f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Unlike the Apple non-fragile runtimes, which also uses
2262f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // unwind-based zero cost exceptions, the GNU Objective C runtime's
2263f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // EH support isn't a veneer over C++ EH.  Instead, exception
2264f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // objects are created by __objc_exception_throw and destroyed by
2265f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // the personality function; this avoids the need for bracketing
2266f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // catch handlers with calls to __blah_begin_catch/__blah_end_catch
2267f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // (or even _Unwind_DeleteException), but probably doesn't
2268f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // interoperate very well with foreign exceptions.
22699735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  //
227080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // In Objective-C++ mode, we actually emit something equivalent to the C++
22719735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  // exception handler.
22729735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
22739735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  return ;
227464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
227564d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
22769f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
227749f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar                              const ObjCAtThrowStmt &S) {
22785dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  llvm::Value *ExceptionAsObject;
22795dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
22805dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  if (const Expr *ThrowExpr = S.getThrowExpr()) {
22815dc0867af17b3bd6f567897433853b2b4767446cChris Lattner    llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
22821e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian    ExceptionAsObject = Exception;
22835dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  } else {
22841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
22855dc0867af17b3bd6f567897433853b2b4767446cChris Lattner           "Unexpected rethrow outside @catch block.");
22865dc0867af17b3bd6f567897433853b2b4767446cChris Lattner    ExceptionAsObject = CGF.ObjCEHValueStack.back();
22875dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  }
22881e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian  ExceptionAsObject =
22891e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian      CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
22901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22915dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // Note: This may have to be an invoke, if we want to support constructs like:
22925dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // @try {
22935dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  //  @throw(obj);
22945dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // }
22955dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // @catch(id) ...
22965dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  //
22975dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // This is effectively turning @throw into an incredibly-expensive goto, but
22985dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // it may happen as a result of inlining followed by missed optimizations, or
22995dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // as a result of stupidity.
23005dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
23015dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  if (!UnwindBB) {
23029f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    CGF.Builder.CreateCall(ExceptionThrowFn, ExceptionAsObject);
23035dc0867af17b3bd6f567897433853b2b4767446cChris Lattner    CGF.Builder.CreateUnreachable();
23045dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  } else {
23054c7d9f1507d0f102bd4133bba63348636facd469Jay Foad    CGF.Builder.CreateInvoke(ExceptionThrowFn, UnwindBB, UnwindBB,
23064c7d9f1507d0f102bd4133bba63348636facd469Jay Foad                             ExceptionAsObject);
23075dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  }
23085dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  // Clear the insertion point to indicate we are in unreachable code.
23095dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  CGF.Builder.ClearInsertionPoint();
231064d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
231164d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
23129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
23131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          llvm::Value *AddrWeakObj) {
2314ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
231531fc0c1a16dabd397dd3323fb0dbe7052b87abd0David Chisnall  AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy);
2316ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  return B.CreateCall(WeakReadFn, AddrWeakObj);
23176dc2317b59cb1180a59f6c283d96b7a5dfeb5307Fariborz Jahanian}
23186dc2317b59cb1180a59f6c283d96b7a5dfeb5307Fariborz Jahanian
23199f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
23201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                   llvm::Value *src, llvm::Value *dst) {
2321ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2322ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2323ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2324ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall2(WeakAssignFn, src, dst);
23253e283e344595e0bd499b13b30a92b7d9c10a2140Fariborz Jahanian}
23263e283e344595e0bd499b13b30a92b7d9c10a2140Fariborz Jahanian
23279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
2328021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                     llvm::Value *src, llvm::Value *dst,
2329021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                     bool threadlocal) {
2330ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2331ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2332ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2333021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian  if (!threadlocal)
2334021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian    B.CreateCall2(GlobalAssignFn, src, dst);
2335021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian  else
2336021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian    // FIXME. Add threadloca assign API
2337021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian    assert(false && "EmitObjCGlobalAssign - Threal Local API NYI");
233858626500527695865683d1d65053743de8770b60Fariborz Jahanian}
233958626500527695865683d1d65053743de8770b60Fariborz Jahanian
23409f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
23416c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                   llvm::Value *src, llvm::Value *dst,
23426c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                   llvm::Value *ivarOffset) {
2343ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2344ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2345b44eda31a214ad88616acbe28c3f0a70268adc6cDavid Chisnall  dst = EnforceType(B, dst, IdTy);
2346ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall3(IvarAssignFn, src, dst, ivarOffset);
23477eda8367cf63caee8acf907356b1d199ccaa6e89Fariborz Jahanian}
23487eda8367cf63caee8acf907356b1d199ccaa6e89Fariborz Jahanian
23499f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
23501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                         llvm::Value *src, llvm::Value *dst) {
2351ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
2352ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2353ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2354ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall2(StrongCastAssignFn, src, dst);
235558626500527695865683d1d65053743de8770b60Fariborz Jahanian}
235658626500527695865683d1d65053743de8770b60Fariborz Jahanian
23579f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
23581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                         llvm::Value *DestPtr,
2359082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian                                         llvm::Value *SrcPtr,
236055bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian                                         llvm::Value *Size) {
2361ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  CGBuilderTy B = CGF.Builder;
236268e5e1331d99140ae5dd7b27c3dd28618827dfc8David Chisnall  DestPtr = EnforceType(B, DestPtr, PtrTy);
236368e5e1331d99140ae5dd7b27c3dd28618827dfc8David Chisnall  SrcPtr = EnforceType(B, SrcPtr, PtrTy);
2364ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
236555bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian  B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size);
2366082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian}
2367082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian
23689cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanianllvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
23699cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                              const ObjCInterfaceDecl *ID,
23709cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                              const ObjCIvarDecl *Ivar) {
23719cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
23729cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    + '.' + Ivar->getNameAsString();
23739cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // Emit the variable and initialize it with what we think the correct value
23749cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // is.  This allows code compiled with non-fragile ivars to work correctly
23759cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // when linked against code which isn't (most of the time).
23768a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
23778a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  if (!IvarOffsetPointer) {
2378e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // This will cause a run-time crash if we accidentally use it.  A value of
2379e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // 0 would seem more sensible, but will silently overwrite the isa pointer
2380e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // causing a great deal of confusion.
2381e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    uint64_t Offset = -1;
2382e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // We can't call ComputeIvarBaseOffset() here if we have the
2383e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // implementation, because it will create an invalid ASTRecordLayout object
2384e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // that we are then stuck with forever, so we only initialize the ivar
2385e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // offset variable with a guess if we only have the interface.  The
2386e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // initializer will be reset later anyway, when we are generating the class
2387e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // description.
2388e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    if (!CGM.getContext().getObjCImplementation(
2389cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman              const_cast<ObjCInterfaceDecl *>(ID)))
2390d901da531433254210a08e8b1f9e1ad049b340aaDavid Chisnall      Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
2391d901da531433254210a08e8b1f9e1ad049b340aaDavid Chisnall
23929cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    llvm::ConstantInt *OffsetGuess =
2393f950837b8250c7f08d0b7b15ace0b35740721c9dDavid Chisnall      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar");
23948a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // Don't emit the guess in non-PIC code because the linker will not be able
23958a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // to replace it with the real version for a library.  In non-PIC code you
23968a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // must compile with the fragile ABI if you want to use ivars from a
23971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // GCC-compiled class.
23988a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    if (CGM.getLangOptions().PICLevel) {
23998a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
24008a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            llvm::Type::getInt32Ty(VMContext), false,
24018a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
24028a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
24038a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
24048a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            IvarOffsetGV, Name);
24058a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    } else {
24068a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
24073c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer              llvm::Type::getInt32PtrTy(VMContext), false,
24083c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer              llvm::GlobalValue::ExternalLinkage, 0, Name);
24098a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    }
24109cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
24118a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  return IvarOffsetPointer;
24129cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian}
24139cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian
24149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallLValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
2415598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       QualType ObjectTy,
2416598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       llvm::Value *BaseValue,
2417598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       const ObjCIvarDecl *Ivar,
2418598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       unsigned CVRQualifiers) {
2419c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl *ID =
2420c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjectTy->getAs<ObjCObjectType>()->getInterface();
24219777687562c338601c2f17906e65e1c1a0aad96fDaniel Dunbar  return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
24229777687562c338601c2f17906e65e1c1a0aad96fDaniel Dunbar                                  EmitIvarOffset(CGF, ID, Ivar));
24230bb20361a321593887f067515dd04cf109f4c74aFariborz Jahanian}
2424bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
24259cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanianstatic const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
24269cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                                                  const ObjCInterfaceDecl *OID,
24279cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                                                  const ObjCIvarDecl *OIVD) {
2428db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next;
2429db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose       next = next->getNextIvar()) {
2430db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    if (OIVD == next)
24319cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      return OID;
24329cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
24331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24349cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // Otherwise check in the super class.
24359cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
24369cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    return FindIvarInterface(Context, Super, OIVD);
24371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24389cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  return 0;
24399cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian}
24400bb20361a321593887f067515dd04cf109f4c74aFariborz Jahanian
24419f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
24422a03192a02dbf4fdff438d1e658356bde871aba4Daniel Dunbar                         const ObjCInterfaceDecl *Interface,
2443f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian                         const ObjCIvarDecl *Ivar) {
24448a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  if (CGM.getLangOptions().ObjCNonFragileABI) {
24459cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
244663ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    if (RuntimeVersion < 10)
244763ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      return CGF.Builder.CreateZExtOrBitCast(
244863ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
244963ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall                  ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")),
245063ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          PtrDiffTy);
245163ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    std::string name = "__objc_ivar_offset_value_" +
245263ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      Interface->getNameAsString() +"." + Ivar->getNameAsString();
245363ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    llvm::Value *Offset = TheModule.getGlobalVariable(name);
245463ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    if (!Offset)
245563ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      Offset = new llvm::GlobalVariable(TheModule, IntTy,
24563fc81d32314eca98b51846f493a8bc77cf14a701David Chisnall          false, llvm::GlobalValue::LinkOnceAnyLinkage,
24573fc81d32314eca98b51846f493a8bc77cf14a701David Chisnall          llvm::Constant::getNullValue(IntTy), name);
245863ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    return CGF.Builder.CreateLoad(Offset);
24599cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
24609777687562c338601c2f17906e65e1c1a0aad96fDaniel Dunbar  uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
24617e02d1aebb3ee0841f398a836ba5d34be5cdb4b3David Chisnall  return llvm::ConstantInt::get(PtrDiffTy, Offset, "ivar");
2462f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian}
2463f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian
24649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCRuntime *
24659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
24669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  if (CGM.getLangOptions().ObjCNonFragileABI)
24679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    return new CGObjCGNUstep(CGM);
24689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return new CGObjCGCC(CGM);
24690f984268b05edab2cc555a427c441baa9c252658Chris Lattner}
2470