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 16280031Sdim#ifndef LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H 17280031Sdim#define LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H 18193326Sed#include "CGBuilder.h" 19193326Sed#include "CGCall.h" 20193326Sed#include "CGValue.h" 21249423Sdim#include "clang/AST/DeclObjC.h" 22249423Sdim#include "clang/Basic/IdentifierTable.h" // Selector 23193326Sed 24193326Sednamespace llvm { 25193326Sed class Constant; 26193326Sed class Function; 27193326Sed class Module; 28193326Sed class StructLayout; 29193326Sed class StructType; 30193326Sed class Type; 31193326Sed class Value; 32193326Sed} 33193326Sed 34193326Sednamespace clang { 35193326Sednamespace CodeGen { 36193326Sed class CodeGenFunction; 37193326Sed} 38193326Sed 39193326Sed class FieldDecl; 40193326Sed class ObjCAtTryStmt; 41193326Sed class ObjCAtThrowStmt; 42193326Sed class ObjCAtSynchronizedStmt; 43193326Sed class ObjCContainerDecl; 44193326Sed class ObjCCategoryImplDecl; 45193326Sed class ObjCImplementationDecl; 46193326Sed class ObjCInterfaceDecl; 47193326Sed class ObjCMessageExpr; 48193326Sed class ObjCMethodDecl; 49193326Sed class ObjCProtocolDecl; 50193326Sed class Selector; 51193326Sed class ObjCIvarDecl; 52193326Sed class ObjCStringLiteral; 53212904Sdim class BlockDeclRefExpr; 54193326Sed 55193326Sednamespace CodeGen { 56193326Sed class CodeGenModule; 57218893Sdim class CGBlockInfo; 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: 65234353Sdim CodeGen::CodeGenModule &CGM; 66234353Sdim CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {} 67234353Sdim 68193326Sed // Utility functions for unified ivar access. These need to 69193326Sed // eventually be folded into other places (the structure layout 70193326Sed // code). 71193326Sed 72193326Sed /// Compute an offset to the given ivar, suitable for passing to 73193326Sed /// EmitValueForIvarAtOffset. Note that the correct handling of 74193326Sed /// bit-fields is carefully coordinated by these two, use caution! 75193326Sed /// 76193326Sed /// The latter overload is suitable for computing the offset of a 77193326Sed /// sythesized ivar. 78193326Sed uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 79193326Sed const ObjCInterfaceDecl *OID, 80193326Sed const ObjCIvarDecl *Ivar); 81193326Sed uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 82193326Sed const ObjCImplementationDecl *OID, 83193326Sed const ObjCIvarDecl *Ivar); 84193326Sed 85193326Sed LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, 86193326Sed const ObjCInterfaceDecl *OID, 87193326Sed llvm::Value *BaseValue, 88193326Sed const ObjCIvarDecl *Ivar, 89193326Sed unsigned CVRQualifiers, 90198092Srdivacky llvm::Value *Offset); 91221345Sdim /// Emits a try / catch statement. This function is intended to be called by 92221345Sdim /// subclasses, and provides a generic mechanism for generating these, which 93239462Sdim /// should be usable by all runtimes. The caller must provide the functions 94239462Sdim /// to call when entering and exiting a \@catch() block, and the function 95239462Sdim /// used to rethrow exceptions. If the begin and end catch functions are 96239462Sdim /// NULL, then the function assumes that the EH personality function provides 97239462Sdim /// the thrown object directly. 98221345Sdim void EmitTryCatchStmt(CodeGenFunction &CGF, 99221345Sdim const ObjCAtTryStmt &S, 100223017Sdim llvm::Constant *beginCatchFn, 101223017Sdim llvm::Constant *endCatchFn, 102223017Sdim llvm::Constant *exceptionRethrowFn); 103296417Sdim 104296417Sdim void EmitInitOfCatchParam(CodeGenFunction &CGF, llvm::Value *exn, 105296417Sdim const VarDecl *paramDecl); 106296417Sdim 107239462Sdim /// Emits an \@synchronize() statement, using the \p syncEnterFn and 108239462Sdim /// \p syncExitFn arguments as the functions called to lock and unlock 109239462Sdim /// the object. This function can be called by subclasses that use 110239462Sdim /// zero-cost exception handling. 111221345Sdim void EmitAtSynchronizedStmt(CodeGenFunction &CGF, 112221345Sdim const ObjCAtSynchronizedStmt &S, 113221345Sdim llvm::Function *syncEnterFn, 114221345Sdim llvm::Function *syncExitFn); 115193326Sed 116193326Sedpublic: 117193326Sed virtual ~CGObjCRuntime(); 118193326Sed 119193326Sed /// Generate the function required to register all Objective-C components in 120193326Sed /// this compilation unit with the runtime library. 121193326Sed virtual llvm::Function *ModuleInitFunction() = 0; 122193326Sed 123296417Sdim /// Get a selector for the specified name and type values. 124296417Sdim /// The result should have the LLVM type for ASTContext::getObjCSelType(). 125296417Sdim virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) = 0; 126296417Sdim 127296417Sdim /// Get the address of a selector for the specified name and type values. 128296417Sdim /// This is a rarely-used language extension, but sadly it exists. 129296417Sdim /// 130296417Sdim /// The result should have the LLVM type for a pointer to 131193326Sed /// ASTContext::getObjCSelType(). 132296417Sdim virtual Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) = 0; 133193326Sed 134198092Srdivacky /// Get a typed selector. 135249423Sdim virtual llvm::Value *GetSelector(CodeGenFunction &CGF, 136193326Sed const ObjCMethodDecl *Method) = 0; 137193326Sed 138212904Sdim /// Get the type constant to catch for the given ObjC pointer type. 139212904Sdim /// This is used externally to implement catching ObjC types in C++. 140212904Sdim /// Runtimes which don't support this should add the appropriate 141212904Sdim /// error to Sema. 142212904Sdim virtual llvm::Constant *GetEHType(QualType T) = 0; 143212904Sdim 144193326Sed /// Generate a constant string object. 145296417Sdim virtual ConstantAddress GenerateConstantString(const StringLiteral *) = 0; 146234353Sdim 147193326Sed /// Generate a category. A category contains a list of methods (and 148193326Sed /// accompanying metadata) and a list of protocols. 149193326Sed virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0; 150193326Sed 151221345Sdim /// Generate a class structure for this class. 152193326Sed virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0; 153198092Srdivacky 154234353Sdim /// Register an class alias. 155234353Sdim virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0; 156234353Sdim 157198092Srdivacky /// Generate an Objective-C message send operation. 158198092Srdivacky /// 159198092Srdivacky /// \param Method - The method being called, this may be null if synthesizing 160198092Srdivacky /// a property setter or getter. 161198092Srdivacky virtual CodeGen::RValue 162193326Sed GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 163208600Srdivacky ReturnValueSlot ReturnSlot, 164193326Sed QualType ResultType, 165193326Sed Selector Sel, 166193326Sed llvm::Value *Receiver, 167193326Sed const CallArgList &CallArgs, 168276479Sdim const ObjCInterfaceDecl *Class = nullptr, 169276479Sdim const ObjCMethodDecl *Method = nullptr) = 0; 170193326Sed 171193326Sed /// Generate an Objective-C message send operation to the super 172193326Sed /// class initiated in a method for Class and with the given Self 173193326Sed /// object. 174198092Srdivacky /// 175198092Srdivacky /// \param Method - The method being called, this may be null if synthesizing 176198092Srdivacky /// a property setter or getter. 177193326Sed virtual CodeGen::RValue 178193326Sed GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 179208600Srdivacky ReturnValueSlot ReturnSlot, 180193326Sed QualType ResultType, 181193326Sed Selector Sel, 182193326Sed const ObjCInterfaceDecl *Class, 183193326Sed bool isCategoryImpl, 184193326Sed llvm::Value *Self, 185193326Sed bool IsClassMessage, 186198092Srdivacky const CallArgList &CallArgs, 187276479Sdim const ObjCMethodDecl *Method = nullptr) = 0; 188193326Sed 189193326Sed /// Emit the code to return the named protocol as an object, as in a 190239462Sdim /// \@protocol expression. 191249423Sdim virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, 192193326Sed const ObjCProtocolDecl *OPD) = 0; 193193326Sed 194198092Srdivacky /// Generate the named protocol. Protocols contain method metadata but no 195198092Srdivacky /// implementations. 196193326Sed virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0; 197193326Sed 198193326Sed /// Generate a function preamble for a method with the specified 199198092Srdivacky /// types. 200193326Sed 201193326Sed // FIXME: Current this just generates the Function definition, but really this 202193326Sed // should also be generating the loads of the parameters, as the runtime 203193326Sed // should have full control over how parameters are passed. 204198092Srdivacky virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 205193326Sed const ObjCContainerDecl *CD) = 0; 206193326Sed 207193326Sed /// Return the runtime function for getting properties. 208193326Sed virtual llvm::Constant *GetPropertyGetFunction() = 0; 209198092Srdivacky 210193326Sed /// Return the runtime function for setting properties. 211193326Sed virtual llvm::Constant *GetPropertySetFunction() = 0; 212193326Sed 213234353Sdim /// Return the runtime function for optimized setting properties. 214234353Sdim virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, 215234353Sdim bool copy) = 0; 216234353Sdim 217218893Sdim // API for atomic copying of qualified aggregates in getter. 218218893Sdim virtual llvm::Constant *GetGetStructFunction() = 0; 219218893Sdim // API for atomic copying of qualified aggregates in setter. 220218893Sdim virtual llvm::Constant *GetSetStructFunction() = 0; 221249423Sdim /// API for atomic copying of qualified aggregates with non-trivial copy 222249423Sdim /// assignment (c++) in setter. 223249423Sdim virtual llvm::Constant *GetCppAtomicObjectSetFunction() = 0; 224249423Sdim /// API for atomic copying of qualified aggregates with non-trivial copy 225249423Sdim /// assignment (c++) in getter. 226249423Sdim virtual llvm::Constant *GetCppAtomicObjectGetFunction() = 0; 227207619Srdivacky 228193326Sed /// GetClass - Return a reference to the class for the given 229193326Sed /// interface decl. 230249423Sdim virtual llvm::Value *GetClass(CodeGenFunction &CGF, 231193326Sed const ObjCInterfaceDecl *OID) = 0; 232224145Sdim 233224145Sdim 234249423Sdim virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) { 235226633Sdim llvm_unreachable("autoreleasepool unsupported in this ABI"); 236224145Sdim } 237224145Sdim 238193326Sed /// EnumerationMutationFunction - Return the function that's called by the 239193326Sed /// compiler when a mutation is detected during foreach iteration. 240193326Sed virtual llvm::Constant *EnumerationMutationFunction() = 0; 241198092Srdivacky 242210299Sed virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 243210299Sed const ObjCAtSynchronizedStmt &S) = 0; 244210299Sed virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, 245210299Sed const ObjCAtTryStmt &S) = 0; 246193326Sed virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 247249423Sdim const ObjCAtThrowStmt &S, 248249423Sdim bool ClearInsertionPoint=true) = 0; 249193326Sed virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 250296417Sdim Address AddrWeakObj) = 0; 251193326Sed virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 252296417Sdim llvm::Value *src, Address dest) = 0; 253193326Sed virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 254296417Sdim llvm::Value *src, Address dest, 255212904Sdim bool threadlocal=false) = 0; 256193326Sed virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 257296417Sdim llvm::Value *src, Address dest, 258198092Srdivacky llvm::Value *ivarOffset) = 0; 259193326Sed virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 260296417Sdim llvm::Value *src, Address dest) = 0; 261198092Srdivacky 262193326Sed virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 263193326Sed QualType ObjectTy, 264193326Sed llvm::Value *BaseValue, 265193326Sed const ObjCIvarDecl *Ivar, 266193326Sed unsigned CVRQualifiers) = 0; 267193326Sed virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 268193326Sed const ObjCInterfaceDecl *Interface, 269193326Sed const ObjCIvarDecl *Ivar) = 0; 270198092Srdivacky virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, 271296417Sdim Address DestPtr, 272296417Sdim Address SrcPtr, 273210299Sed llvm::Value *Size) = 0; 274218893Sdim virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, 275218893Sdim const CodeGen::CGBlockInfo &blockInfo) = 0; 276243830Sdim virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, 277243830Sdim const CodeGen::CGBlockInfo &blockInfo) = 0; 278296417Sdim 279296417Sdim /// Returns an i8* which points to the byref layout information. 280249423Sdim virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM, 281249423Sdim QualType T) = 0; 282296417Sdim 283276479Sdim virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name, 284276479Sdim bool Weak = false) = 0; 285234353Sdim 286234353Sdim struct MessageSendInfo { 287234353Sdim const CGFunctionInfo &CallInfo; 288234353Sdim llvm::PointerType *MessengerType; 289234353Sdim 290234353Sdim MessageSendInfo(const CGFunctionInfo &callInfo, 291234353Sdim llvm::PointerType *messengerType) 292234353Sdim : CallInfo(callInfo), MessengerType(messengerType) {} 293234353Sdim }; 294234353Sdim 295234353Sdim MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method, 296234353Sdim QualType resultType, 297234353Sdim CallArgList &callArgs); 298243830Sdim 299243830Sdim // FIXME: This probably shouldn't be here, but the code to compute 300243830Sdim // it is here. 301243830Sdim unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM, 302243830Sdim const ObjCInterfaceDecl *ID, 303243830Sdim const ObjCIvarDecl *Ivar); 304193326Sed}; 305193326Sed 306198092Srdivacky/// Creates an instance of an Objective-C runtime class. 307193326Sed//TODO: This should include some way of selecting which runtime to target. 308193326SedCGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM); 309193326SedCGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM); 310193326Sed} 311193326Sed} 312193326Sed#endif 313