1193326Sed//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===// 2193326Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193326Sed// 7193326Sed//===----------------------------------------------------------------------===// 8193326Sed// 9193326Sed// This provides an abstract class for Objective-C code generation. Concrete 10193326Sed// subclasses of this implement code generation for specific Objective-C 11193326Sed// runtime libraries. 12193326Sed// 13193326Sed//===----------------------------------------------------------------------===// 14193326Sed 15280031Sdim#ifndef LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H 16280031Sdim#define LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H 17193326Sed#include "CGBuilder.h" 18193326Sed#include "CGCall.h" 19341825Sdim#include "CGCleanup.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. 98353358Sdim void EmitTryCatchStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S, 99353358Sdim llvm::FunctionCallee beginCatchFn, 100353358Sdim llvm::FunctionCallee endCatchFn, 101353358Sdim llvm::FunctionCallee exceptionRethrowFn); 102296417Sdim 103296417Sdim void EmitInitOfCatchParam(CodeGenFunction &CGF, llvm::Value *exn, 104296417Sdim const VarDecl *paramDecl); 105296417Sdim 106239462Sdim /// Emits an \@synchronize() statement, using the \p syncEnterFn and 107239462Sdim /// \p syncExitFn arguments as the functions called to lock and unlock 108239462Sdim /// the object. This function can be called by subclasses that use 109239462Sdim /// zero-cost exception handling. 110221345Sdim void EmitAtSynchronizedStmt(CodeGenFunction &CGF, 111353358Sdim const ObjCAtSynchronizedStmt &S, 112353358Sdim llvm::FunctionCallee syncEnterFn, 113353358Sdim llvm::FunctionCallee syncExitFn); 114193326Sed 115193326Sedpublic: 116193326Sed virtual ~CGObjCRuntime(); 117193326Sed 118193326Sed /// Generate the function required to register all Objective-C components in 119193326Sed /// this compilation unit with the runtime library. 120193326Sed virtual llvm::Function *ModuleInitFunction() = 0; 121193326Sed 122296417Sdim /// Get a selector for the specified name and type values. 123296417Sdim /// The result should have the LLVM type for ASTContext::getObjCSelType(). 124296417Sdim virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) = 0; 125296417Sdim 126296417Sdim /// Get the address of a selector for the specified name and type values. 127296417Sdim /// This is a rarely-used language extension, but sadly it exists. 128296417Sdim /// 129296417Sdim /// The result should have the LLVM type for a pointer to 130193326Sed /// ASTContext::getObjCSelType(). 131296417Sdim virtual Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) = 0; 132193326Sed 133198092Srdivacky /// Get a typed selector. 134249423Sdim virtual llvm::Value *GetSelector(CodeGenFunction &CGF, 135193326Sed const ObjCMethodDecl *Method) = 0; 136193326Sed 137212904Sdim /// Get the type constant to catch for the given ObjC pointer type. 138212904Sdim /// This is used externally to implement catching ObjC types in C++. 139212904Sdim /// Runtimes which don't support this should add the appropriate 140212904Sdim /// error to Sema. 141212904Sdim virtual llvm::Constant *GetEHType(QualType T) = 0; 142212904Sdim 143341825Sdim virtual CatchTypeInfo getCatchAllTypeInfo() { return { nullptr, 0 }; } 144341825Sdim 145193326Sed /// Generate a constant string object. 146296417Sdim virtual ConstantAddress GenerateConstantString(const StringLiteral *) = 0; 147341825Sdim 148193326Sed /// Generate a category. A category contains a list of methods (and 149193326Sed /// accompanying metadata) and a list of protocols. 150193326Sed virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0; 151193326Sed 152221345Sdim /// Generate a class structure for this class. 153193326Sed virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0; 154198092Srdivacky 155234353Sdim /// Register an class alias. 156234353Sdim virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0; 157234353Sdim 158198092Srdivacky /// Generate an Objective-C message send operation. 159198092Srdivacky /// 160198092Srdivacky /// \param Method - The method being called, this may be null if synthesizing 161198092Srdivacky /// a property setter or getter. 162198092Srdivacky virtual CodeGen::RValue 163193326Sed GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 164208600Srdivacky ReturnValueSlot ReturnSlot, 165193326Sed QualType ResultType, 166193326Sed Selector Sel, 167193326Sed llvm::Value *Receiver, 168193326Sed const CallArgList &CallArgs, 169276479Sdim const ObjCInterfaceDecl *Class = nullptr, 170276479Sdim const ObjCMethodDecl *Method = nullptr) = 0; 171193326Sed 172360784Sdim /// Generate an Objective-C message send operation. 173360784Sdim /// 174360784Sdim /// This variant allows for the call to be substituted with an optimized 175360784Sdim /// variant. 176360784Sdim CodeGen::RValue 177360784Sdim GeneratePossiblySpecializedMessageSend(CodeGenFunction &CGF, 178360784Sdim ReturnValueSlot Return, 179360784Sdim QualType ResultType, 180360784Sdim Selector Sel, 181360784Sdim llvm::Value *Receiver, 182360784Sdim const CallArgList& Args, 183360784Sdim const ObjCInterfaceDecl *OID, 184360784Sdim const ObjCMethodDecl *Method, 185360784Sdim bool isClassMessage); 186360784Sdim 187193326Sed /// Generate an Objective-C message send operation to the super 188193326Sed /// class initiated in a method for Class and with the given Self 189193326Sed /// object. 190198092Srdivacky /// 191198092Srdivacky /// \param Method - The method being called, this may be null if synthesizing 192198092Srdivacky /// a property setter or getter. 193193326Sed virtual CodeGen::RValue 194193326Sed GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 195208600Srdivacky ReturnValueSlot ReturnSlot, 196193326Sed QualType ResultType, 197193326Sed Selector Sel, 198193326Sed const ObjCInterfaceDecl *Class, 199193326Sed bool isCategoryImpl, 200193326Sed llvm::Value *Self, 201193326Sed bool IsClassMessage, 202198092Srdivacky const CallArgList &CallArgs, 203276479Sdim const ObjCMethodDecl *Method = nullptr) = 0; 204193326Sed 205193326Sed /// Emit the code to return the named protocol as an object, as in a 206239462Sdim /// \@protocol expression. 207249423Sdim virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, 208193326Sed const ObjCProtocolDecl *OPD) = 0; 209193326Sed 210198092Srdivacky /// Generate the named protocol. Protocols contain method metadata but no 211198092Srdivacky /// implementations. 212193326Sed virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0; 213193326Sed 214193326Sed /// Generate a function preamble for a method with the specified 215198092Srdivacky /// types. 216193326Sed 217193326Sed // FIXME: Current this just generates the Function definition, but really this 218193326Sed // should also be generating the loads of the parameters, as the runtime 219193326Sed // should have full control over how parameters are passed. 220198092Srdivacky virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 221193326Sed const ObjCContainerDecl *CD) = 0; 222193326Sed 223360784Sdim /// Generates prologue for direct Objective-C Methods. 224360784Sdim virtual void GenerateDirectMethodPrologue(CodeGenFunction &CGF, 225360784Sdim llvm::Function *Fn, 226360784Sdim const ObjCMethodDecl *OMD, 227360784Sdim const ObjCContainerDecl *CD) = 0; 228360784Sdim 229193326Sed /// Return the runtime function for getting properties. 230353358Sdim virtual llvm::FunctionCallee GetPropertyGetFunction() = 0; 231198092Srdivacky 232193326Sed /// Return the runtime function for setting properties. 233353358Sdim virtual llvm::FunctionCallee GetPropertySetFunction() = 0; 234193326Sed 235234353Sdim /// Return the runtime function for optimized setting properties. 236353358Sdim virtual llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic, 237353358Sdim bool copy) = 0; 238234353Sdim 239218893Sdim // API for atomic copying of qualified aggregates in getter. 240353358Sdim virtual llvm::FunctionCallee GetGetStructFunction() = 0; 241218893Sdim // API for atomic copying of qualified aggregates in setter. 242353358Sdim virtual llvm::FunctionCallee GetSetStructFunction() = 0; 243249423Sdim /// API for atomic copying of qualified aggregates with non-trivial copy 244249423Sdim /// assignment (c++) in setter. 245353358Sdim virtual llvm::FunctionCallee GetCppAtomicObjectSetFunction() = 0; 246249423Sdim /// API for atomic copying of qualified aggregates with non-trivial copy 247249423Sdim /// assignment (c++) in getter. 248353358Sdim virtual llvm::FunctionCallee GetCppAtomicObjectGetFunction() = 0; 249341825Sdim 250193326Sed /// GetClass - Return a reference to the class for the given 251193326Sed /// interface decl. 252249423Sdim virtual llvm::Value *GetClass(CodeGenFunction &CGF, 253193326Sed const ObjCInterfaceDecl *OID) = 0; 254341825Sdim 255341825Sdim 256249423Sdim virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) { 257226633Sdim llvm_unreachable("autoreleasepool unsupported in this ABI"); 258224145Sdim } 259341825Sdim 260193326Sed /// EnumerationMutationFunction - Return the function that's called by the 261193326Sed /// compiler when a mutation is detected during foreach iteration. 262353358Sdim virtual llvm::FunctionCallee EnumerationMutationFunction() = 0; 263198092Srdivacky 264210299Sed virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 265210299Sed const ObjCAtSynchronizedStmt &S) = 0; 266210299Sed virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, 267210299Sed const ObjCAtTryStmt &S) = 0; 268193326Sed virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 269249423Sdim const ObjCAtThrowStmt &S, 270249423Sdim bool ClearInsertionPoint=true) = 0; 271193326Sed virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 272296417Sdim Address AddrWeakObj) = 0; 273193326Sed virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 274296417Sdim llvm::Value *src, Address dest) = 0; 275193326Sed virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 276296417Sdim llvm::Value *src, Address dest, 277212904Sdim bool threadlocal=false) = 0; 278193326Sed virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 279296417Sdim llvm::Value *src, Address dest, 280198092Srdivacky llvm::Value *ivarOffset) = 0; 281193326Sed virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 282296417Sdim llvm::Value *src, Address dest) = 0; 283198092Srdivacky 284193326Sed virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 285193326Sed QualType ObjectTy, 286193326Sed llvm::Value *BaseValue, 287193326Sed const ObjCIvarDecl *Ivar, 288193326Sed unsigned CVRQualifiers) = 0; 289193326Sed virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 290193326Sed const ObjCInterfaceDecl *Interface, 291193326Sed const ObjCIvarDecl *Ivar) = 0; 292198092Srdivacky virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, 293296417Sdim Address DestPtr, 294296417Sdim Address SrcPtr, 295210299Sed llvm::Value *Size) = 0; 296218893Sdim virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, 297218893Sdim const CodeGen::CGBlockInfo &blockInfo) = 0; 298243830Sdim virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, 299243830Sdim const CodeGen::CGBlockInfo &blockInfo) = 0; 300344779Sdim virtual std::string getRCBlockLayoutStr(CodeGen::CodeGenModule &CGM, 301344779Sdim const CGBlockInfo &blockInfo) { 302344779Sdim return {}; 303344779Sdim } 304296417Sdim 305296417Sdim /// Returns an i8* which points to the byref layout information. 306249423Sdim virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM, 307249423Sdim QualType T) = 0; 308296417Sdim 309234353Sdim struct MessageSendInfo { 310234353Sdim const CGFunctionInfo &CallInfo; 311234353Sdim llvm::PointerType *MessengerType; 312234353Sdim 313234353Sdim MessageSendInfo(const CGFunctionInfo &callInfo, 314234353Sdim llvm::PointerType *messengerType) 315234353Sdim : CallInfo(callInfo), MessengerType(messengerType) {} 316234353Sdim }; 317234353Sdim 318234353Sdim MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method, 319234353Sdim QualType resultType, 320234353Sdim CallArgList &callArgs); 321243830Sdim 322243830Sdim // FIXME: This probably shouldn't be here, but the code to compute 323243830Sdim // it is here. 324243830Sdim unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM, 325243830Sdim const ObjCInterfaceDecl *ID, 326243830Sdim const ObjCIvarDecl *Ivar); 327193326Sed}; 328193326Sed 329198092Srdivacky/// Creates an instance of an Objective-C runtime class. 330193326Sed//TODO: This should include some way of selecting which runtime to target. 331193326SedCGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM); 332193326SedCGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM); 333193326Sed} 334193326Sed} 335193326Sed#endif 336