CGObjCRuntime.h revision 46f45b9bec4a265ad8400a538e5ec3a5683617f1
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This provides an abstract class for Objective-C code generation.  Concrete
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// subclasses of this implement code generation for specific Objective-C
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// runtime libraries.
135e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)//
145e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)//===----------------------------------------------------------------------===//
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CLANG_CODEGEN_OBCJRUNTIME_H
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CLANG_CODEGEN_OBCJRUNTIME_H
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/IdentifierTable.h" // Selector
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/SmallVector.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/IRBuilder.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "CGValue.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "CGCall.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace llvm {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Constant;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Type;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Value;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Module;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Function;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace clang {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace CodeGen {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class CodeGenFunction;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ObjCCategoryImplDecl;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ObjCImplementationDecl;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ObjCInterfaceDecl;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ObjCMessageExpr;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ObjCMethodDecl;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ObjCProtocolDecl;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Selector;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace CodeGen {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class CodeGenModule;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//FIXME Several methods should be pure virtual but aren't to avoid the
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//partially-implemented subclass breaking.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Implements runtime-specific code generation functions.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CGObjCRuntime {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef llvm::IRBuilder<> BuilderType;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~CGObjCRuntime();
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Generate the function required to register all Objective-C components in
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// this compilation unit with the runtime library.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual llvm::Function *ModuleInitFunction() = 0;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Get a selector for the specified name and type values. The
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// return value should have the LLVM type for pointer-to
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ASTContext::getObjCSelType().
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual llvm::Value *GetSelector(BuilderType &Builder,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   Selector Sel) = 0;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Generate a constant string object.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Generate a category.  A category contains a list of methods (and
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// accompanying metadata) and a list of protocols.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Generate a class stucture for this class.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Generate an Objective-C message send operation.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual CodeGen::RValue
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      QualType ResultType,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      Selector Sel,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      llvm::Value *Receiver,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      bool IsClassMessage,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const CallArgList &CallArgs) = 0;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Generate an Objective-C message send operation to the super
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// class initiated in a method for Class and with the given Self
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// object.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual CodeGen::RValue
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           QualType ResultType,
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           Selector Sel,
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           const ObjCInterfaceDecl *Class,
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           llvm::Value *Self,
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool IsClassMessage,
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           const CallArgList &CallArgs) = 0;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Emit the code to return the named protocol as an object, as in a
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// @protocol expression.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           const ObjCProtocolDecl *OPD) = 0;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Generate the named protocol.  Protocols contain method metadata but no
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// implementations.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Generate a function preamble for a method with the specified
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// types.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FIXME: Current this just generates the Function definition, but
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // really this should also be generating the loads of the
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parameters, as the runtime should have full control over how
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parameters are passed.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD) = 0;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// GetClass - Return a reference to the class for the given
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// interface decl.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual llvm::Value *GetClass(BuilderType &Builder,
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const ObjCInterfaceDecl *OID) = 0;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// EnumerationMutationFunction - Return the function that's called by the
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// compiler when a mutation is detected during foreach iteration.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual llvm::Function *EnumerationMutationFunction() = 0;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// If instance variable addresses are determined at runtime then this should
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// return true, otherwise instance variables will be accessed directly from
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// the structure.  If this returns true then @defs is invalid for this
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// runtime and a warning should be generated.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool LateBoundIVars() const { return false; }
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Creates an instance of an Objective-C runtime class.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//TODO: This should include some way of selecting which runtime to target.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)