CGObjCRuntime.h revision 208600
1193326Sed//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This provides an abstract class for Objective-C code generation. Concrete 11193326Sed// subclasses of this implement code generation for specific Objective-C 12193326Sed// runtime libraries. 13193326Sed// 14193326Sed//===----------------------------------------------------------------------===// 15193326Sed 16193326Sed#ifndef CLANG_CODEGEN_OBCJRUNTIME_H 17193326Sed#define CLANG_CODEGEN_OBCJRUNTIME_H 18193326Sed#include "clang/Basic/IdentifierTable.h" // Selector 19193326Sed#include "clang/AST/DeclObjC.h" 20193326Sed#include <string> 21193326Sed 22193326Sed#include "CGBuilder.h" 23193326Sed#include "CGCall.h" 24193326Sed#include "CGValue.h" 25193326Sed 26193326Sednamespace llvm { 27193326Sed class Constant; 28193326Sed class Function; 29193326Sed class Module; 30193326Sed class StructLayout; 31193326Sed class StructType; 32193326Sed class Type; 33193326Sed class Value; 34193326Sed} 35193326Sed 36193326Sednamespace clang { 37193326Sednamespace CodeGen { 38193326Sed class CodeGenFunction; 39193326Sed} 40193326Sed 41193326Sed class FieldDecl; 42193326Sed class ObjCAtTryStmt; 43193326Sed class ObjCAtThrowStmt; 44193326Sed class ObjCAtSynchronizedStmt; 45193326Sed class ObjCContainerDecl; 46193326Sed class ObjCCategoryImplDecl; 47193326Sed class ObjCImplementationDecl; 48193326Sed class ObjCInterfaceDecl; 49193326Sed class ObjCMessageExpr; 50193326Sed class ObjCMethodDecl; 51193326Sed class ObjCProtocolDecl; 52193326Sed class Selector; 53193326Sed class ObjCIvarDecl; 54193326Sed class ObjCStringLiteral; 55193326Sed 56193326Sednamespace CodeGen { 57193326Sed class CodeGenModule; 58193326Sed 59193326Sed// FIXME: Several methods should be pure virtual but aren't to avoid the 60193326Sed// partially-implemented subclass breaking. 61193326Sed 62193326Sed/// Implements runtime-specific code generation functions. 63193326Sedclass CGObjCRuntime { 64206275Srdivackyprotected: 65193326Sed // Utility functions for unified ivar access. These need to 66193326Sed // eventually be folded into other places (the structure layout 67193326Sed // code). 68193326Sed 69193326Sed /// Compute an offset to the given ivar, suitable for passing to 70193326Sed /// EmitValueForIvarAtOffset. Note that the correct handling of 71193326Sed /// bit-fields is carefully coordinated by these two, use caution! 72193326Sed /// 73193326Sed /// The latter overload is suitable for computing the offset of a 74193326Sed /// sythesized ivar. 75193326Sed uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 76193326Sed const ObjCInterfaceDecl *OID, 77193326Sed const ObjCIvarDecl *Ivar); 78193326Sed uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 79193326Sed const ObjCImplementationDecl *OID, 80193326Sed const ObjCIvarDecl *Ivar); 81193326Sed 82193326Sed LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, 83193326Sed const ObjCInterfaceDecl *OID, 84193326Sed llvm::Value *BaseValue, 85193326Sed const ObjCIvarDecl *Ivar, 86193326Sed unsigned CVRQualifiers, 87198092Srdivacky llvm::Value *Offset); 88193326Sed 89193326Sedpublic: 90193326Sed virtual ~CGObjCRuntime(); 91193326Sed 92193326Sed /// Generate the function required to register all Objective-C components in 93193326Sed /// this compilation unit with the runtime library. 94193326Sed virtual llvm::Function *ModuleInitFunction() = 0; 95193326Sed 96193326Sed /// Get a selector for the specified name and type values. The 97193326Sed /// return value should have the LLVM type for pointer-to 98193326Sed /// ASTContext::getObjCSelType(). 99193326Sed virtual llvm::Value *GetSelector(CGBuilderTy &Builder, 100193326Sed Selector Sel) = 0; 101193326Sed 102198092Srdivacky /// Get a typed selector. 103193326Sed virtual llvm::Value *GetSelector(CGBuilderTy &Builder, 104193326Sed const ObjCMethodDecl *Method) = 0; 105193326Sed 106193326Sed /// Generate a constant string object. 107202879Srdivacky virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0; 108193326Sed 109193326Sed /// Generate a category. A category contains a list of methods (and 110193326Sed /// accompanying metadata) and a list of protocols. 111193326Sed virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0; 112193326Sed 113193326Sed /// Generate a class stucture for this class. 114193326Sed virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0; 115198092Srdivacky 116198092Srdivacky /// Generate an Objective-C message send operation. 117198092Srdivacky /// 118198092Srdivacky /// \param Method - The method being called, this may be null if synthesizing 119198092Srdivacky /// a property setter or getter. 120198092Srdivacky virtual CodeGen::RValue 121193326Sed GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 122208600Srdivacky ReturnValueSlot ReturnSlot, 123193326Sed QualType ResultType, 124193326Sed Selector Sel, 125193326Sed llvm::Value *Receiver, 126193326Sed const CallArgList &CallArgs, 127207619Srdivacky const ObjCInterfaceDecl *Class = 0, 128198092Srdivacky const ObjCMethodDecl *Method = 0) = 0; 129193326Sed 130193326Sed /// Generate an Objective-C message send operation to the super 131193326Sed /// class initiated in a method for Class and with the given Self 132193326Sed /// object. 133198092Srdivacky /// 134198092Srdivacky /// \param Method - The method being called, this may be null if synthesizing 135198092Srdivacky /// a property setter or getter. 136193326Sed virtual CodeGen::RValue 137193326Sed GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 138208600Srdivacky ReturnValueSlot ReturnSlot, 139193326Sed QualType ResultType, 140193326Sed Selector Sel, 141193326Sed const ObjCInterfaceDecl *Class, 142193326Sed bool isCategoryImpl, 143193326Sed llvm::Value *Self, 144193326Sed bool IsClassMessage, 145198092Srdivacky const CallArgList &CallArgs, 146198092Srdivacky const ObjCMethodDecl *Method = 0) = 0; 147193326Sed 148193326Sed /// Emit the code to return the named protocol as an object, as in a 149193326Sed /// @protocol expression. 150193326Sed virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 151193326Sed const ObjCProtocolDecl *OPD) = 0; 152193326Sed 153198092Srdivacky /// Generate the named protocol. Protocols contain method metadata but no 154198092Srdivacky /// implementations. 155193326Sed virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0; 156193326Sed 157193326Sed /// Generate a function preamble for a method with the specified 158198092Srdivacky /// types. 159193326Sed 160193326Sed // FIXME: Current this just generates the Function definition, but really this 161193326Sed // should also be generating the loads of the parameters, as the runtime 162193326Sed // should have full control over how parameters are passed. 163198092Srdivacky virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 164193326Sed const ObjCContainerDecl *CD) = 0; 165193326Sed 166193326Sed /// Return the runtime function for getting properties. 167193326Sed virtual llvm::Constant *GetPropertyGetFunction() = 0; 168198092Srdivacky 169193326Sed /// Return the runtime function for setting properties. 170193326Sed virtual llvm::Constant *GetPropertySetFunction() = 0; 171193326Sed 172207619Srdivacky // API for atomic copying of qualified aggregates in setter/getter. 173207619Srdivacky virtual llvm::Constant *GetCopyStructFunction() = 0; 174207619Srdivacky 175193326Sed /// GetClass - Return a reference to the class for the given 176193326Sed /// interface decl. 177198092Srdivacky virtual llvm::Value *GetClass(CGBuilderTy &Builder, 178193326Sed const ObjCInterfaceDecl *OID) = 0; 179193326Sed 180193326Sed /// EnumerationMutationFunction - Return the function that's called by the 181193326Sed /// compiler when a mutation is detected during foreach iteration. 182193326Sed virtual llvm::Constant *EnumerationMutationFunction() = 0; 183198092Srdivacky 184193326Sed virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 185193326Sed const Stmt &S) = 0; 186193326Sed virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 187193326Sed const ObjCAtThrowStmt &S) = 0; 188193326Sed virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 189193326Sed llvm::Value *AddrWeakObj) = 0; 190193326Sed virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 191193326Sed llvm::Value *src, llvm::Value *dest) = 0; 192193326Sed virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 193193326Sed llvm::Value *src, llvm::Value *dest) = 0; 194193326Sed virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 195198092Srdivacky llvm::Value *src, llvm::Value *dest, 196198092Srdivacky llvm::Value *ivarOffset) = 0; 197193326Sed virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 198193326Sed llvm::Value *src, llvm::Value *dest) = 0; 199198092Srdivacky 200193326Sed virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 201193326Sed QualType ObjectTy, 202193326Sed llvm::Value *BaseValue, 203193326Sed const ObjCIvarDecl *Ivar, 204193326Sed unsigned CVRQualifiers) = 0; 205193326Sed virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 206193326Sed const ObjCInterfaceDecl *Interface, 207193326Sed const ObjCIvarDecl *Ivar) = 0; 208198092Srdivacky virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, 209198092Srdivacky llvm::Value *DestPtr, 210198092Srdivacky llvm::Value *SrcPtr, 211198092Srdivacky QualType Ty) = 0; 212193326Sed}; 213193326Sed 214198092Srdivacky/// Creates an instance of an Objective-C runtime class. 215193326Sed//TODO: This should include some way of selecting which runtime to target. 216193326SedCGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM); 217193326SedCGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM); 218193326SedCGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM); 219193326Sed} 220193326Sed} 221193326Sed#endif 222