Path: blob/main/contrib/llvm-project/clang/lib/CodeGen/CGObjCRuntime.h
35233 views
//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This provides an abstract class for Objective-C code generation. Concrete9// subclasses of this implement code generation for specific Objective-C10// runtime libraries.11//12//===----------------------------------------------------------------------===//1314#ifndef LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H15#define LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H16#include "CGBuilder.h"17#include "CGCall.h"18#include "CGCleanup.h"19#include "CGValue.h"20#include "clang/AST/DeclObjC.h"21#include "clang/Basic/IdentifierTable.h" // Selector22#include "llvm/ADT/UniqueVector.h"2324namespace llvm {25class Constant;26class Function;27class Module;28class StructLayout;29class StructType;30class Type;31class Value;32}3334namespace clang {35namespace CodeGen {36class CGFunctionInfo;37class CodeGenFunction;38}3940class FieldDecl;41class ObjCAtTryStmt;42class ObjCAtThrowStmt;43class ObjCAtSynchronizedStmt;44class ObjCContainerDecl;45class ObjCCategoryImplDecl;46class ObjCImplementationDecl;47class ObjCInterfaceDecl;48class ObjCMessageExpr;49class ObjCMethodDecl;50class ObjCProtocolDecl;51class Selector;52class ObjCIvarDecl;53class ObjCStringLiteral;54class BlockDeclRefExpr;5556namespace CodeGen {57class CodeGenModule;58class CGBlockInfo;5960// FIXME: Several methods should be pure virtual but aren't to avoid the61// partially-implemented subclass breaking.6263/// Implements runtime-specific code generation functions.64class CGObjCRuntime {65protected:66CodeGen::CodeGenModule &CGM;67CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {}6869// Utility functions for unified ivar access. These need to70// eventually be folded into other places (the structure layout71// code).7273/// Compute an offset to the given ivar, suitable for passing to74/// EmitValueForIvarAtOffset. Note that the correct handling of75/// bit-fields is carefully coordinated by these two, use caution!76///77/// The latter overload is suitable for computing the offset of a78/// sythesized ivar.79uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,80const ObjCInterfaceDecl *OID,81const ObjCIvarDecl *Ivar);82uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,83const ObjCImplementationDecl *OID,84const ObjCIvarDecl *Ivar);8586LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,87const ObjCInterfaceDecl *OID,88llvm::Value *BaseValue,89const ObjCIvarDecl *Ivar,90unsigned CVRQualifiers,91llvm::Value *Offset);92/// Emits a try / catch statement. This function is intended to be called by93/// subclasses, and provides a generic mechanism for generating these, which94/// should be usable by all runtimes. The caller must provide the functions95/// to call when entering and exiting a \@catch() block, and the function96/// used to rethrow exceptions. If the begin and end catch functions are97/// NULL, then the function assumes that the EH personality function provides98/// the thrown object directly.99void EmitTryCatchStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S,100llvm::FunctionCallee beginCatchFn,101llvm::FunctionCallee endCatchFn,102llvm::FunctionCallee exceptionRethrowFn);103104void EmitInitOfCatchParam(CodeGenFunction &CGF, llvm::Value *exn,105const VarDecl *paramDecl);106107/// Emits an \@synchronize() statement, using the \p syncEnterFn and108/// \p syncExitFn arguments as the functions called to lock and unlock109/// the object. This function can be called by subclasses that use110/// zero-cost exception handling.111void EmitAtSynchronizedStmt(CodeGenFunction &CGF,112const ObjCAtSynchronizedStmt &S,113llvm::FunctionCallee syncEnterFn,114llvm::FunctionCallee syncExitFn);115116public:117virtual ~CGObjCRuntime();118119std::string getSymbolNameForMethod(const ObjCMethodDecl *method,120bool includeCategoryName = true);121122/// Generate the function required to register all Objective-C components in123/// this compilation unit with the runtime library.124virtual llvm::Function *ModuleInitFunction() = 0;125126/// Get a selector for the specified name and type values.127/// The result should have the LLVM type for ASTContext::getObjCSelType().128virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) = 0;129130/// Get the address of a selector for the specified name and type values.131/// This is a rarely-used language extension, but sadly it exists.132///133/// The result should have the LLVM type for a pointer to134/// ASTContext::getObjCSelType().135virtual Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) = 0;136137/// Get a typed selector.138virtual llvm::Value *GetSelector(CodeGenFunction &CGF,139const ObjCMethodDecl *Method) = 0;140141/// Get the type constant to catch for the given ObjC pointer type.142/// This is used externally to implement catching ObjC types in C++.143/// Runtimes which don't support this should add the appropriate144/// error to Sema.145virtual llvm::Constant *GetEHType(QualType T) = 0;146147virtual CatchTypeInfo getCatchAllTypeInfo() { return { nullptr, 0 }; }148149/// Generate a constant string object.150virtual ConstantAddress GenerateConstantString(const StringLiteral *) = 0;151152/// Generate a category. A category contains a list of methods (and153/// accompanying metadata) and a list of protocols.154virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;155156/// Generate a class structure for this class.157virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;158159/// Register an class alias.160virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;161162/// Generate an Objective-C message send operation.163///164/// \param Method - The method being called, this may be null if synthesizing165/// a property setter or getter.166virtual CodeGen::RValue167GenerateMessageSend(CodeGen::CodeGenFunction &CGF,168ReturnValueSlot ReturnSlot,169QualType ResultType,170Selector Sel,171llvm::Value *Receiver,172const CallArgList &CallArgs,173const ObjCInterfaceDecl *Class = nullptr,174const ObjCMethodDecl *Method = nullptr) = 0;175176/// Generate an Objective-C message send operation.177///178/// This variant allows for the call to be substituted with an optimized179/// variant.180CodeGen::RValue181GeneratePossiblySpecializedMessageSend(CodeGenFunction &CGF,182ReturnValueSlot Return,183QualType ResultType,184Selector Sel,185llvm::Value *Receiver,186const CallArgList& Args,187const ObjCInterfaceDecl *OID,188const ObjCMethodDecl *Method,189bool isClassMessage);190191/// Generate an Objective-C message send operation to the super192/// class initiated in a method for Class and with the given Self193/// object.194///195/// \param Method - The method being called, this may be null if synthesizing196/// a property setter or getter.197virtual CodeGen::RValue198GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,199ReturnValueSlot ReturnSlot,200QualType ResultType,201Selector Sel,202const ObjCInterfaceDecl *Class,203bool isCategoryImpl,204llvm::Value *Self,205bool IsClassMessage,206const CallArgList &CallArgs,207const ObjCMethodDecl *Method = nullptr) = 0;208209/// Walk the list of protocol references from a class, category or210/// protocol to traverse the DAG formed from it's inheritance hierarchy. Find211/// the list of protocols that ends each walk at either a runtime212/// protocol or a non-runtime protocol with no parents. For the common case of213/// just a list of standard runtime protocols this just returns the same list214/// that was passed in.215std::vector<const ObjCProtocolDecl *>216GetRuntimeProtocolList(ObjCProtocolDecl::protocol_iterator begin,217ObjCProtocolDecl::protocol_iterator end);218219/// Emit the code to return the named protocol as an object, as in a220/// \@protocol expression.221virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,222const ObjCProtocolDecl *OPD) = 0;223224/// Generate the named protocol. Protocols contain method metadata but no225/// implementations.226virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;227228/// GetOrEmitProtocol - Get the protocol object for the given229/// declaration, emitting it if necessary. The return value has type230/// ProtocolPtrTy.231virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;232233/// Generate a function preamble for a method with the specified234/// types.235236// FIXME: Current this just generates the Function definition, but really this237// should also be generating the loads of the parameters, as the runtime238// should have full control over how parameters are passed.239virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,240const ObjCContainerDecl *CD) = 0;241242/// Generates prologue for direct Objective-C Methods.243virtual void GenerateDirectMethodPrologue(CodeGenFunction &CGF,244llvm::Function *Fn,245const ObjCMethodDecl *OMD,246const ObjCContainerDecl *CD) = 0;247248/// Return the runtime function for getting properties.249virtual llvm::FunctionCallee GetPropertyGetFunction() = 0;250251/// Return the runtime function for setting properties.252virtual llvm::FunctionCallee GetPropertySetFunction() = 0;253254/// Return the runtime function for optimized setting properties.255virtual llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic,256bool copy) = 0;257258// API for atomic copying of qualified aggregates in getter.259virtual llvm::FunctionCallee GetGetStructFunction() = 0;260// API for atomic copying of qualified aggregates in setter.261virtual llvm::FunctionCallee GetSetStructFunction() = 0;262/// API for atomic copying of qualified aggregates with non-trivial copy263/// assignment (c++) in setter.264virtual llvm::FunctionCallee GetCppAtomicObjectSetFunction() = 0;265/// API for atomic copying of qualified aggregates with non-trivial copy266/// assignment (c++) in getter.267virtual llvm::FunctionCallee GetCppAtomicObjectGetFunction() = 0;268269/// GetClass - Return a reference to the class for the given270/// interface decl.271virtual llvm::Value *GetClass(CodeGenFunction &CGF,272const ObjCInterfaceDecl *OID) = 0;273274275virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {276llvm_unreachable("autoreleasepool unsupported in this ABI");277}278279/// EnumerationMutationFunction - Return the function that's called by the280/// compiler when a mutation is detected during foreach iteration.281virtual llvm::FunctionCallee EnumerationMutationFunction() = 0;282283virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,284const ObjCAtSynchronizedStmt &S) = 0;285virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,286const ObjCAtTryStmt &S) = 0;287virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,288const ObjCAtThrowStmt &S,289bool ClearInsertionPoint=true) = 0;290virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,291Address AddrWeakObj) = 0;292virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,293llvm::Value *src, Address dest) = 0;294virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,295llvm::Value *src, Address dest,296bool threadlocal=false) = 0;297virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,298llvm::Value *src, Address dest,299llvm::Value *ivarOffset) = 0;300virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,301llvm::Value *src, Address dest) = 0;302303virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,304QualType ObjectTy,305llvm::Value *BaseValue,306const ObjCIvarDecl *Ivar,307unsigned CVRQualifiers) = 0;308virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,309const ObjCInterfaceDecl *Interface,310const ObjCIvarDecl *Ivar) = 0;311virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,312Address DestPtr,313Address SrcPtr,314llvm::Value *Size) = 0;315virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,316const CodeGen::CGBlockInfo &blockInfo) = 0;317virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,318const CodeGen::CGBlockInfo &blockInfo) = 0;319virtual std::string getRCBlockLayoutStr(CodeGen::CodeGenModule &CGM,320const CGBlockInfo &blockInfo) {321return {};322}323324/// Returns an i8* which points to the byref layout information.325virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,326QualType T) = 0;327328struct MessageSendInfo {329const CGFunctionInfo &CallInfo;330llvm::PointerType *MessengerType;331332MessageSendInfo(const CGFunctionInfo &callInfo,333llvm::PointerType *messengerType)334: CallInfo(callInfo), MessengerType(messengerType) {}335};336337MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,338QualType resultType,339CallArgList &callArgs);340bool canMessageReceiverBeNull(CodeGenFunction &CGF,341const ObjCMethodDecl *method,342bool isSuper,343const ObjCInterfaceDecl *classReceiver,344llvm::Value *receiver);345static bool isWeakLinkedClass(const ObjCInterfaceDecl *cls);346347/// Destroy the callee-destroyed arguments of the given method,348/// if it has any. Used for nil-receiver paths in message sends.349/// Never does anything if the method does not satisfy350/// hasParamDestroyedInCallee().351///352/// \param callArgs - just the formal arguments, not including implicit353/// arguments such as self and cmd354static void destroyCalleeDestroyedArguments(CodeGenFunction &CGF,355const ObjCMethodDecl *method,356const CallArgList &callArgs);357358// FIXME: This probably shouldn't be here, but the code to compute359// it is here.360unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,361const ObjCInterfaceDecl *ID,362const ObjCIvarDecl *Ivar);363};364365/// Creates an instance of an Objective-C runtime class.366//TODO: This should include some way of selecting which runtime to target.367CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);368CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);369}370}371#endif372373374