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 "clang/Basic/IdentifierTable.h" // Selector
19 #include "clang/AST/DeclObjC.h"
21 #include "CGBuilder.h"
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 to
95 /// call when entering and exiting a @catch() block, and the function used to
96 /// rethrow exceptions. If the begin and end catch functions are NULL, then
97 /// the function assumes that the EH personality function provides the
98 /// thrown object directly.
99 void EmitTryCatchStmt(CodeGenFunction &CGF,
100 const ObjCAtTryStmt &S,
101 llvm::Constant *beginCatchFn,
102 llvm::Constant *endCatchFn,
103 llvm::Constant *exceptionRethrowFn);
104 /// Emits an @synchronize() statement, using the syncEnterFn and syncExitFn
105 /// arguments as the functions called to lock and unlock the object. This
106 /// function can be called by subclasses that use zero-cost exception
108 void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
109 const ObjCAtSynchronizedStmt &S,
110 llvm::Function *syncEnterFn,
111 llvm::Function *syncExitFn);
114 virtual ~CGObjCRuntime();
116 /// Generate the function required to register all Objective-C components in
117 /// this compilation unit with the runtime library.
118 virtual llvm::Function *ModuleInitFunction() = 0;
120 /// Get a selector for the specified name and type values. The
121 /// return value should have the LLVM type for pointer-to
122 /// ASTContext::getObjCSelType().
123 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
124 Selector Sel, bool lval=false) = 0;
126 /// Get a typed selector.
127 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
128 const ObjCMethodDecl *Method) = 0;
130 /// Get the type constant to catch for the given ObjC pointer type.
131 /// This is used externally to implement catching ObjC types in C++.
132 /// Runtimes which don't support this should add the appropriate
134 virtual llvm::Constant *GetEHType(QualType T) = 0;
136 /// Generate a constant string object.
137 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
139 /// Generate a category. A category contains a list of methods (and
140 /// accompanying metadata) and a list of protocols.
141 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
143 /// Generate a class structure for this class.
144 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
146 /// Register an class alias.
147 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
149 /// Generate an Objective-C message send operation.
151 /// \param Method - The method being called, this may be null if synthesizing
152 /// a property setter or getter.
153 virtual CodeGen::RValue
154 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
155 ReturnValueSlot ReturnSlot,
158 llvm::Value *Receiver,
159 const CallArgList &CallArgs,
160 const ObjCInterfaceDecl *Class = 0,
161 const ObjCMethodDecl *Method = 0) = 0;
163 /// Generate an Objective-C message send operation to the super
164 /// class initiated in a method for Class and with the given Self
167 /// \param Method - The method being called, this may be null if synthesizing
168 /// a property setter or getter.
169 virtual CodeGen::RValue
170 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
171 ReturnValueSlot ReturnSlot,
174 const ObjCInterfaceDecl *Class,
178 const CallArgList &CallArgs,
179 const ObjCMethodDecl *Method = 0) = 0;
181 /// Emit the code to return the named protocol as an object, as in a
182 /// @protocol expression.
183 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
184 const ObjCProtocolDecl *OPD) = 0;
186 /// Generate the named protocol. Protocols contain method metadata but no
188 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
190 /// Generate a function preamble for a method with the specified
193 // FIXME: Current this just generates the Function definition, but really this
194 // should also be generating the loads of the parameters, as the runtime
195 // should have full control over how parameters are passed.
196 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
197 const ObjCContainerDecl *CD) = 0;
199 /// Return the runtime function for getting properties.
200 virtual llvm::Constant *GetPropertyGetFunction() = 0;
202 /// Return the runtime function for setting properties.
203 virtual llvm::Constant *GetPropertySetFunction() = 0;
205 /// Return the runtime function for optimized setting properties.
206 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
209 // API for atomic copying of qualified aggregates in getter.
210 virtual llvm::Constant *GetGetStructFunction() = 0;
211 // API for atomic copying of qualified aggregates in setter.
212 virtual llvm::Constant *GetSetStructFunction() = 0;
213 // API for atomic copying of qualified aggregates with non-trivial copy
214 // assignment (c++) in setter/getter.
215 virtual llvm::Constant *GetCppAtomicObjectFunction() = 0;
217 /// GetClass - Return a reference to the class for the given
219 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
220 const ObjCInterfaceDecl *OID) = 0;
223 virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
224 llvm_unreachable("autoreleasepool unsupported in this ABI");
227 /// EnumerationMutationFunction - Return the function that's called by the
228 /// compiler when a mutation is detected during foreach iteration.
229 virtual llvm::Constant *EnumerationMutationFunction() = 0;
231 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
232 const ObjCAtSynchronizedStmt &S) = 0;
233 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
234 const ObjCAtTryStmt &S) = 0;
235 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
236 const ObjCAtThrowStmt &S) = 0;
237 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
238 llvm::Value *AddrWeakObj) = 0;
239 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
240 llvm::Value *src, llvm::Value *dest) = 0;
241 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
242 llvm::Value *src, llvm::Value *dest,
243 bool threadlocal=false) = 0;
244 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
245 llvm::Value *src, llvm::Value *dest,
246 llvm::Value *ivarOffset) = 0;
247 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
248 llvm::Value *src, llvm::Value *dest) = 0;
250 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
252 llvm::Value *BaseValue,
253 const ObjCIvarDecl *Ivar,
254 unsigned CVRQualifiers) = 0;
255 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
256 const ObjCInterfaceDecl *Interface,
257 const ObjCIvarDecl *Ivar) = 0;
258 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
259 llvm::Value *DestPtr,
261 llvm::Value *Size) = 0;
262 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
263 const CodeGen::CGBlockInfo &blockInfo) = 0;
264 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) = 0;
266 struct MessageSendInfo {
267 const CGFunctionInfo &CallInfo;
268 llvm::PointerType *MessengerType;
270 MessageSendInfo(const CGFunctionInfo &callInfo,
271 llvm::PointerType *messengerType)
272 : CallInfo(callInfo), MessengerType(messengerType) {}
275 MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
277 CallArgList &callArgs);
280 /// Creates an instance of an Objective-C runtime class.
281 //TODO: This should include some way of selecting which runtime to target.
282 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
283 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);