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 // Utility functions for unified ivar access. These need to
67 // eventually be folded into other places (the structure layout
70 /// Compute an offset to the given ivar, suitable for passing to
71 /// EmitValueForIvarAtOffset. Note that the correct handling of
72 /// bit-fields is carefully coordinated by these two, use caution!
74 /// The latter overload is suitable for computing the offset of a
76 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
77 const ObjCInterfaceDecl *OID,
78 const ObjCIvarDecl *Ivar);
79 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
80 const ObjCImplementationDecl *OID,
81 const ObjCIvarDecl *Ivar);
83 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
84 const ObjCInterfaceDecl *OID,
85 llvm::Value *BaseValue,
86 const ObjCIvarDecl *Ivar,
87 unsigned CVRQualifiers,
89 /// Emits a try / catch statement. This function is intended to be called by
90 /// subclasses, and provides a generic mechanism for generating these, which
91 /// should be usable by all runtimes. The caller must provide the functions to
92 /// call when entering and exiting a @catch() block, and the function used to
93 /// rethrow exceptions. If the begin and end catch functions are NULL, then
94 /// the function assumes that the EH personality function provides the
95 /// thrown object directly.
96 void EmitTryCatchStmt(CodeGenFunction &CGF,
97 const ObjCAtTryStmt &S,
98 llvm::Constant *beginCatchFn,
99 llvm::Constant *endCatchFn,
100 llvm::Constant *exceptionRethrowFn);
101 /// Emits an @synchronize() statement, using the syncEnterFn and syncExitFn
102 /// arguments as the functions called to lock and unlock the object. This
103 /// function can be called by subclasses that use zero-cost exception
105 void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
106 const ObjCAtSynchronizedStmt &S,
107 llvm::Function *syncEnterFn,
108 llvm::Function *syncExitFn);
111 virtual ~CGObjCRuntime();
113 /// Generate the function required to register all Objective-C components in
114 /// this compilation unit with the runtime library.
115 virtual llvm::Function *ModuleInitFunction() = 0;
117 /// Get a selector for the specified name and type values. The
118 /// return value should have the LLVM type for pointer-to
119 /// ASTContext::getObjCSelType().
120 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
121 Selector Sel, bool lval=false) = 0;
123 /// Get a typed selector.
124 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
125 const ObjCMethodDecl *Method) = 0;
127 /// Get the type constant to catch for the given ObjC pointer type.
128 /// This is used externally to implement catching ObjC types in C++.
129 /// Runtimes which don't support this should add the appropriate
131 virtual llvm::Constant *GetEHType(QualType T) = 0;
133 /// Generate a constant string object.
134 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
136 /// Generate a category. A category contains a list of methods (and
137 /// accompanying metadata) and a list of protocols.
138 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
140 /// Generate a class structure for this class.
141 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
143 /// Register an class alias.
144 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
146 /// Generate an Objective-C message send operation.
148 /// \param Method - The method being called, this may be null if synthesizing
149 /// a property setter or getter.
150 virtual CodeGen::RValue
151 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
152 ReturnValueSlot ReturnSlot,
155 llvm::Value *Receiver,
156 const CallArgList &CallArgs,
157 const ObjCInterfaceDecl *Class = 0,
158 const ObjCMethodDecl *Method = 0) = 0;
160 /// Generate an Objective-C message send operation to the super
161 /// class initiated in a method for Class and with the given Self
164 /// \param Method - The method being called, this may be null if synthesizing
165 /// a property setter or getter.
166 virtual CodeGen::RValue
167 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
168 ReturnValueSlot ReturnSlot,
171 const ObjCInterfaceDecl *Class,
175 const CallArgList &CallArgs,
176 const ObjCMethodDecl *Method = 0) = 0;
178 /// Emit the code to return the named protocol as an object, as in a
179 /// @protocol expression.
180 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
181 const ObjCProtocolDecl *OPD) = 0;
183 /// Generate the named protocol. Protocols contain method metadata but no
185 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
187 /// Generate a function preamble for a method with the specified
190 // FIXME: Current this just generates the Function definition, but really this
191 // should also be generating the loads of the parameters, as the runtime
192 // should have full control over how parameters are passed.
193 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
194 const ObjCContainerDecl *CD) = 0;
196 /// Return the runtime function for getting properties.
197 virtual llvm::Constant *GetPropertyGetFunction() = 0;
199 /// Return the runtime function for setting properties.
200 virtual llvm::Constant *GetPropertySetFunction() = 0;
202 // API for atomic copying of qualified aggregates in getter.
203 virtual llvm::Constant *GetGetStructFunction() = 0;
204 // API for atomic copying of qualified aggregates in setter.
205 virtual llvm::Constant *GetSetStructFunction() = 0;
206 // API for atomic copying of qualified aggregates with non-trivial copy
207 // assignment (c++) in setter/getter.
208 virtual llvm::Constant *GetCppAtomicObjectFunction() = 0;
210 /// GetClass - Return a reference to the class for the given
212 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
213 const ObjCInterfaceDecl *OID) = 0;
216 virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
217 llvm_unreachable("autoreleasepool unsupported in this ABI");
220 /// EnumerationMutationFunction - Return the function that's called by the
221 /// compiler when a mutation is detected during foreach iteration.
222 virtual llvm::Constant *EnumerationMutationFunction() = 0;
224 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
225 const ObjCAtSynchronizedStmt &S) = 0;
226 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
227 const ObjCAtTryStmt &S) = 0;
228 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
229 const ObjCAtThrowStmt &S) = 0;
230 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
231 llvm::Value *AddrWeakObj) = 0;
232 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
233 llvm::Value *src, llvm::Value *dest) = 0;
234 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
235 llvm::Value *src, llvm::Value *dest,
236 bool threadlocal=false) = 0;
237 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
238 llvm::Value *src, llvm::Value *dest,
239 llvm::Value *ivarOffset) = 0;
240 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
241 llvm::Value *src, llvm::Value *dest) = 0;
243 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
245 llvm::Value *BaseValue,
246 const ObjCIvarDecl *Ivar,
247 unsigned CVRQualifiers) = 0;
248 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
249 const ObjCInterfaceDecl *Interface,
250 const ObjCIvarDecl *Ivar) = 0;
251 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
252 llvm::Value *DestPtr,
254 llvm::Value *Size) = 0;
255 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
256 const CodeGen::CGBlockInfo &blockInfo) = 0;
257 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) = 0;
260 /// Creates an instance of an Objective-C runtime class.
261 //TODO: This should include some way of selecting which runtime to target.
262 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
263 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);