1 //===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides an abstract class for Objective-C code generation. Concrete
11 // subclasses of this implement code generation for specific Objective-C
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
17 #define LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
18 #include "CGBuilder.h"
20 #include "CGCleanup.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/Basic/IdentifierTable.h" // Selector
37 class CodeGenFunction;
42 class ObjCAtThrowStmt;
43 class ObjCAtSynchronizedStmt;
44 class ObjCContainerDecl;
45 class ObjCCategoryImplDecl;
46 class ObjCImplementationDecl;
47 class ObjCInterfaceDecl;
48 class ObjCMessageExpr;
50 class ObjCProtocolDecl;
53 class ObjCStringLiteral;
54 class BlockDeclRefExpr;
60 // FIXME: Several methods should be pure virtual but aren't to avoid the
61 // partially-implemented subclass breaking.
63 /// Implements runtime-specific code generation functions.
66 CodeGen::CodeGenModule &CGM;
67 CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {}
69 // Utility functions for unified ivar access. These need to
70 // eventually be folded into other places (the structure layout
73 /// Compute an offset to the given ivar, suitable for passing to
74 /// EmitValueForIvarAtOffset. Note that the correct handling of
75 /// bit-fields is carefully coordinated by these two, use caution!
77 /// The latter overload is suitable for computing the offset of a
79 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
80 const ObjCInterfaceDecl *OID,
81 const ObjCIvarDecl *Ivar);
82 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
83 const ObjCImplementationDecl *OID,
84 const ObjCIvarDecl *Ivar);
86 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
87 const ObjCInterfaceDecl *OID,
88 llvm::Value *BaseValue,
89 const ObjCIvarDecl *Ivar,
90 unsigned CVRQualifiers,
92 /// Emits a try / catch statement. This function is intended to be called by
93 /// subclasses, and provides a generic mechanism for generating these, which
94 /// should be usable by all runtimes. The caller must provide the functions
95 /// to call when entering and exiting a \@catch() block, and the function
96 /// used to rethrow exceptions. If the begin and end catch functions are
97 /// NULL, then the function assumes that the EH personality function provides
98 /// the thrown object directly.
99 void EmitTryCatchStmt(CodeGenFunction &CGF,
100 const ObjCAtTryStmt &S,
101 llvm::Constant *beginCatchFn,
102 llvm::Constant *endCatchFn,
103 llvm::Constant *exceptionRethrowFn);
105 void EmitInitOfCatchParam(CodeGenFunction &CGF, llvm::Value *exn,
106 const VarDecl *paramDecl);
108 /// Emits an \@synchronize() statement, using the \p syncEnterFn and
109 /// \p syncExitFn arguments as the functions called to lock and unlock
110 /// the object. This function can be called by subclasses that use
111 /// zero-cost exception handling.
112 void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
113 const ObjCAtSynchronizedStmt &S,
114 llvm::Function *syncEnterFn,
115 llvm::Function *syncExitFn);
118 virtual ~CGObjCRuntime();
120 /// Generate the function required to register all Objective-C components in
121 /// this compilation unit with the runtime library.
122 virtual llvm::Function *ModuleInitFunction() = 0;
124 /// Get a selector for the specified name and type values.
125 /// The result should have the LLVM type for ASTContext::getObjCSelType().
126 virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) = 0;
128 /// Get the address of a selector for the specified name and type values.
129 /// This is a rarely-used language extension, but sadly it exists.
131 /// The result should have the LLVM type for a pointer to
132 /// ASTContext::getObjCSelType().
133 virtual Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) = 0;
135 /// Get a typed selector.
136 virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
137 const ObjCMethodDecl *Method) = 0;
139 /// Get the type constant to catch for the given ObjC pointer type.
140 /// This is used externally to implement catching ObjC types in C++.
141 /// Runtimes which don't support this should add the appropriate
143 virtual llvm::Constant *GetEHType(QualType T) = 0;
145 virtual CatchTypeInfo getCatchAllTypeInfo() { return { nullptr, 0 }; }
147 /// Generate a constant string object.
148 virtual ConstantAddress GenerateConstantString(const StringLiteral *) = 0;
150 /// Generate a category. A category contains a list of methods (and
151 /// accompanying metadata) and a list of protocols.
152 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
154 /// Generate a class structure for this class.
155 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
157 /// Register an class alias.
158 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
160 /// Generate an Objective-C message send operation.
162 /// \param Method - The method being called, this may be null if synthesizing
163 /// a property setter or getter.
164 virtual CodeGen::RValue
165 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
166 ReturnValueSlot ReturnSlot,
169 llvm::Value *Receiver,
170 const CallArgList &CallArgs,
171 const ObjCInterfaceDecl *Class = nullptr,
172 const ObjCMethodDecl *Method = nullptr) = 0;
174 /// Generate an Objective-C message send operation to the super
175 /// class initiated in a method for Class and with the given Self
178 /// \param Method - The method being called, this may be null if synthesizing
179 /// a property setter or getter.
180 virtual CodeGen::RValue
181 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
182 ReturnValueSlot ReturnSlot,
185 const ObjCInterfaceDecl *Class,
189 const CallArgList &CallArgs,
190 const ObjCMethodDecl *Method = nullptr) = 0;
192 /// Emit the code to return the named protocol as an object, as in a
193 /// \@protocol expression.
194 virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
195 const ObjCProtocolDecl *OPD) = 0;
197 /// Generate the named protocol. Protocols contain method metadata but no
199 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
201 /// Generate a function preamble for a method with the specified
204 // FIXME: Current this just generates the Function definition, but really this
205 // should also be generating the loads of the parameters, as the runtime
206 // should have full control over how parameters are passed.
207 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
208 const ObjCContainerDecl *CD) = 0;
210 /// Return the runtime function for getting properties.
211 virtual llvm::Constant *GetPropertyGetFunction() = 0;
213 /// Return the runtime function for setting properties.
214 virtual llvm::Constant *GetPropertySetFunction() = 0;
216 /// Return the runtime function for optimized setting properties.
217 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
220 // API for atomic copying of qualified aggregates in getter.
221 virtual llvm::Constant *GetGetStructFunction() = 0;
222 // API for atomic copying of qualified aggregates in setter.
223 virtual llvm::Constant *GetSetStructFunction() = 0;
224 /// API for atomic copying of qualified aggregates with non-trivial copy
225 /// assignment (c++) in setter.
226 virtual llvm::Constant *GetCppAtomicObjectSetFunction() = 0;
227 /// API for atomic copying of qualified aggregates with non-trivial copy
228 /// assignment (c++) in getter.
229 virtual llvm::Constant *GetCppAtomicObjectGetFunction() = 0;
231 /// GetClass - Return a reference to the class for the given
233 virtual llvm::Value *GetClass(CodeGenFunction &CGF,
234 const ObjCInterfaceDecl *OID) = 0;
237 virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
238 llvm_unreachable("autoreleasepool unsupported in this ABI");
241 /// EnumerationMutationFunction - Return the function that's called by the
242 /// compiler when a mutation is detected during foreach iteration.
243 virtual llvm::Constant *EnumerationMutationFunction() = 0;
245 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
246 const ObjCAtSynchronizedStmt &S) = 0;
247 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
248 const ObjCAtTryStmt &S) = 0;
249 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
250 const ObjCAtThrowStmt &S,
251 bool ClearInsertionPoint=true) = 0;
252 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
253 Address AddrWeakObj) = 0;
254 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
255 llvm::Value *src, Address dest) = 0;
256 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
257 llvm::Value *src, Address dest,
258 bool threadlocal=false) = 0;
259 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
260 llvm::Value *src, Address dest,
261 llvm::Value *ivarOffset) = 0;
262 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
263 llvm::Value *src, Address dest) = 0;
265 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
267 llvm::Value *BaseValue,
268 const ObjCIvarDecl *Ivar,
269 unsigned CVRQualifiers) = 0;
270 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
271 const ObjCInterfaceDecl *Interface,
272 const ObjCIvarDecl *Ivar) = 0;
273 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
276 llvm::Value *Size) = 0;
277 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
278 const CodeGen::CGBlockInfo &blockInfo) = 0;
279 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
280 const CodeGen::CGBlockInfo &blockInfo) = 0;
282 /// Returns an i8* which points to the byref layout information.
283 virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
286 struct MessageSendInfo {
287 const CGFunctionInfo &CallInfo;
288 llvm::PointerType *MessengerType;
290 MessageSendInfo(const CGFunctionInfo &callInfo,
291 llvm::PointerType *messengerType)
292 : CallInfo(callInfo), MessengerType(messengerType) {}
295 MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
297 CallArgList &callArgs);
299 // FIXME: This probably shouldn't be here, but the code to compute
301 unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,
302 const ObjCInterfaceDecl *ID,
303 const ObjCIvarDecl *Ivar);
306 /// Creates an instance of an Objective-C runtime class.
307 //TODO: This should include some way of selecting which runtime to target.
308 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
309 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);