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 CLANG_CODEGEN_OBCJRUNTIME_H
17 #define CLANG_CODEGEN_OBCJRUNTIME_H
18 #include "CGBuilder.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/Basic/IdentifierTable.h" // Selector
36 class CodeGenFunction;
41 class ObjCAtThrowStmt;
42 class ObjCAtSynchronizedStmt;
43 class ObjCContainerDecl;
44 class ObjCCategoryImplDecl;
45 class ObjCImplementationDecl;
46 class ObjCInterfaceDecl;
47 class ObjCMessageExpr;
49 class ObjCProtocolDecl;
52 class ObjCStringLiteral;
53 class BlockDeclRefExpr;
59 // FIXME: Several methods should be pure virtual but aren't to avoid the
60 // partially-implemented subclass breaking.
62 /// Implements runtime-specific code generation functions.
65 CodeGen::CodeGenModule &CGM;
66 CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {}
68 // Utility functions for unified ivar access. These need to
69 // eventually be folded into other places (the structure layout
72 /// Compute an offset to the given ivar, suitable for passing to
73 /// EmitValueForIvarAtOffset. Note that the correct handling of
74 /// bit-fields is carefully coordinated by these two, use caution!
76 /// The latter overload is suitable for computing the offset of a
78 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
79 const ObjCInterfaceDecl *OID,
80 const ObjCIvarDecl *Ivar);
81 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
82 const ObjCImplementationDecl *OID,
83 const ObjCIvarDecl *Ivar);
85 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
86 const ObjCInterfaceDecl *OID,
87 llvm::Value *BaseValue,
88 const ObjCIvarDecl *Ivar,
89 unsigned CVRQualifiers,
91 /// Emits a try / catch statement. This function is intended to be called by
92 /// subclasses, and provides a generic mechanism for generating these, which
93 /// should be usable by all runtimes. The caller must provide the functions
94 /// to call when entering and exiting a \@catch() block, and the function
95 /// used to rethrow exceptions. If the begin and end catch functions are
96 /// NULL, then the function assumes that the EH personality function provides
97 /// the thrown object directly.
98 void EmitTryCatchStmt(CodeGenFunction &CGF,
99 const ObjCAtTryStmt &S,
100 llvm::Constant *beginCatchFn,
101 llvm::Constant *endCatchFn,
102 llvm::Constant *exceptionRethrowFn);
103 /// Emits an \@synchronize() statement, using the \p syncEnterFn and
104 /// \p syncExitFn arguments as the functions called to lock and unlock
105 /// the object. This function can be called by subclasses that use
106 /// zero-cost exception handling.
107 void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
108 const ObjCAtSynchronizedStmt &S,
109 llvm::Function *syncEnterFn,
110 llvm::Function *syncExitFn);
113 virtual ~CGObjCRuntime();
115 /// Generate the function required to register all Objective-C components in
116 /// this compilation unit with the runtime library.
117 virtual llvm::Function *ModuleInitFunction() = 0;
119 /// Get a selector for the specified name and type values. The
120 /// return value should have the LLVM type for pointer-to
121 /// ASTContext::getObjCSelType().
122 virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
123 Selector Sel, bool lval=false) = 0;
125 /// Get a typed selector.
126 virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
127 const ObjCMethodDecl *Method) = 0;
129 /// Get the type constant to catch for the given ObjC pointer type.
130 /// This is used externally to implement catching ObjC types in C++.
131 /// Runtimes which don't support this should add the appropriate
133 virtual llvm::Constant *GetEHType(QualType T) = 0;
135 /// Generate a constant string object.
136 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
138 /// Generate a category. A category contains a list of methods (and
139 /// accompanying metadata) and a list of protocols.
140 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
142 /// Generate a class structure for this class.
143 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
145 /// Register an class alias.
146 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
148 /// Generate an Objective-C message send operation.
150 /// \param Method - The method being called, this may be null if synthesizing
151 /// a property setter or getter.
152 virtual CodeGen::RValue
153 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
154 ReturnValueSlot ReturnSlot,
157 llvm::Value *Receiver,
158 const CallArgList &CallArgs,
159 const ObjCInterfaceDecl *Class = 0,
160 const ObjCMethodDecl *Method = 0) = 0;
162 /// Generate an Objective-C message send operation to the super
163 /// class initiated in a method for Class and with the given Self
166 /// \param Method - The method being called, this may be null if synthesizing
167 /// a property setter or getter.
168 virtual CodeGen::RValue
169 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
170 ReturnValueSlot ReturnSlot,
173 const ObjCInterfaceDecl *Class,
177 const CallArgList &CallArgs,
178 const ObjCMethodDecl *Method = 0) = 0;
180 /// Emit the code to return the named protocol as an object, as in a
181 /// \@protocol expression.
182 virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
183 const ObjCProtocolDecl *OPD) = 0;
185 /// Generate the named protocol. Protocols contain method metadata but no
187 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
189 /// Generate a function preamble for a method with the specified
192 // FIXME: Current this just generates the Function definition, but really this
193 // should also be generating the loads of the parameters, as the runtime
194 // should have full control over how parameters are passed.
195 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
196 const ObjCContainerDecl *CD) = 0;
198 /// Return the runtime function for getting properties.
199 virtual llvm::Constant *GetPropertyGetFunction() = 0;
201 /// Return the runtime function for setting properties.
202 virtual llvm::Constant *GetPropertySetFunction() = 0;
204 /// Return the runtime function for optimized setting properties.
205 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
208 // API for atomic copying of qualified aggregates in getter.
209 virtual llvm::Constant *GetGetStructFunction() = 0;
210 // API for atomic copying of qualified aggregates in setter.
211 virtual llvm::Constant *GetSetStructFunction() = 0;
212 /// API for atomic copying of qualified aggregates with non-trivial copy
213 /// assignment (c++) in setter.
214 virtual llvm::Constant *GetCppAtomicObjectSetFunction() = 0;
215 /// API for atomic copying of qualified aggregates with non-trivial copy
216 /// assignment (c++) in getter.
217 virtual llvm::Constant *GetCppAtomicObjectGetFunction() = 0;
219 /// GetClass - Return a reference to the class for the given
221 virtual llvm::Value *GetClass(CodeGenFunction &CGF,
222 const ObjCInterfaceDecl *OID) = 0;
225 virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
226 llvm_unreachable("autoreleasepool unsupported in this ABI");
229 /// EnumerationMutationFunction - Return the function that's called by the
230 /// compiler when a mutation is detected during foreach iteration.
231 virtual llvm::Constant *EnumerationMutationFunction() = 0;
233 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
234 const ObjCAtSynchronizedStmt &S) = 0;
235 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
236 const ObjCAtTryStmt &S) = 0;
237 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
238 const ObjCAtThrowStmt &S,
239 bool ClearInsertionPoint=true) = 0;
240 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
241 llvm::Value *AddrWeakObj) = 0;
242 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
243 llvm::Value *src, llvm::Value *dest) = 0;
244 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
245 llvm::Value *src, llvm::Value *dest,
246 bool threadlocal=false) = 0;
247 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
248 llvm::Value *src, llvm::Value *dest,
249 llvm::Value *ivarOffset) = 0;
250 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
251 llvm::Value *src, llvm::Value *dest) = 0;
253 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
255 llvm::Value *BaseValue,
256 const ObjCIvarDecl *Ivar,
257 unsigned CVRQualifiers) = 0;
258 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
259 const ObjCInterfaceDecl *Interface,
260 const ObjCIvarDecl *Ivar) = 0;
261 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
262 llvm::Value *DestPtr,
264 llvm::Value *Size) = 0;
265 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
266 const CodeGen::CGBlockInfo &blockInfo) = 0;
267 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
268 const CodeGen::CGBlockInfo &blockInfo) = 0;
269 virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
271 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) = 0;
273 struct MessageSendInfo {
274 const CGFunctionInfo &CallInfo;
275 llvm::PointerType *MessengerType;
277 MessageSendInfo(const CGFunctionInfo &callInfo,
278 llvm::PointerType *messengerType)
279 : CallInfo(callInfo), MessengerType(messengerType) {}
282 MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
284 CallArgList &callArgs);
286 // FIXME: This probably shouldn't be here, but the code to compute
288 unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,
289 const ObjCInterfaceDecl *ID,
290 const ObjCIvarDecl *Ivar);
293 /// Creates an instance of an Objective-C runtime class.
294 //TODO: This should include some way of selecting which runtime to target.
295 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
296 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);