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"
22 #include "CGBuilder.h"
38 class CodeGenFunction;
43 class ObjCAtThrowStmt;
44 class ObjCAtSynchronizedStmt;
45 class ObjCContainerDecl;
46 class ObjCCategoryImplDecl;
47 class ObjCImplementationDecl;
48 class ObjCInterfaceDecl;
49 class ObjCMessageExpr;
51 class ObjCProtocolDecl;
54 class ObjCStringLiteral;
55 class BlockDeclRefExpr;
61 // FIXME: Several methods should be pure virtual but aren't to avoid the
62 // partially-implemented subclass breaking.
64 /// Implements runtime-specific code generation functions.
67 // Utility functions for unified ivar access. These need to
68 // eventually be folded into other places (the structure layout
71 /// Compute an offset to the given ivar, suitable for passing to
72 /// EmitValueForIvarAtOffset. Note that the correct handling of
73 /// bit-fields is carefully coordinated by these two, use caution!
75 /// The latter overload is suitable for computing the offset of a
77 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
78 const ObjCInterfaceDecl *OID,
79 const ObjCIvarDecl *Ivar);
80 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
81 const ObjCImplementationDecl *OID,
82 const ObjCIvarDecl *Ivar);
84 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
85 const ObjCInterfaceDecl *OID,
86 llvm::Value *BaseValue,
87 const ObjCIvarDecl *Ivar,
88 unsigned CVRQualifiers,
90 /// Emits a try / catch statement. This function is intended to be called by
91 /// subclasses, and provides a generic mechanism for generating these, which
92 /// should be usable by all runtimes. The caller must provide the functions to
93 /// call when entering and exiting a @catch() block, and the function used to
94 /// rethrow exceptions. If the begin and end catch functions are NULL, then
95 /// the function assumes that the EH personality function provides the
96 /// thrown object directly.
97 void EmitTryCatchStmt(CodeGenFunction &CGF,
98 const ObjCAtTryStmt &S,
99 llvm::Function *beginCatchFn,
100 llvm::Function *endCatchFn,
101 llvm::Function *exceptionRethrowFn);
102 /// Emits an @synchronize() statement, using the syncEnterFn and syncExitFn
103 /// arguments as the functions called to lock and unlock the object. This
104 /// function can be called by subclasses that use zero-cost exception
106 void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
107 const ObjCAtSynchronizedStmt &S,
108 llvm::Function *syncEnterFn,
109 llvm::Function *syncExitFn);
112 virtual ~CGObjCRuntime();
114 /// Generate the function required to register all Objective-C components in
115 /// this compilation unit with the runtime library.
116 virtual llvm::Function *ModuleInitFunction() = 0;
118 /// Get a selector for the specified name and type values. The
119 /// return value should have the LLVM type for pointer-to
120 /// ASTContext::getObjCSelType().
121 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
122 Selector Sel, bool lval=false) = 0;
124 /// Get a typed selector.
125 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
126 const ObjCMethodDecl *Method) = 0;
128 /// Get the type constant to catch for the given ObjC pointer type.
129 /// This is used externally to implement catching ObjC types in C++.
130 /// Runtimes which don't support this should add the appropriate
132 virtual llvm::Constant *GetEHType(QualType T) = 0;
134 /// Generate a constant string object.
135 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
137 /// Generate a category. A category contains a list of methods (and
138 /// accompanying metadata) and a list of protocols.
139 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
141 /// Generate a class stucture for this class.
142 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
144 /// Generate an Objective-C message send operation.
146 /// \param Method - The method being called, this may be null if synthesizing
147 /// a property setter or getter.
148 virtual CodeGen::RValue
149 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
150 ReturnValueSlot ReturnSlot,
153 llvm::Value *Receiver,
154 const CallArgList &CallArgs,
155 const ObjCInterfaceDecl *Class = 0,
156 const ObjCMethodDecl *Method = 0) = 0;
158 /// Generate an Objective-C message send operation to the super
159 /// class initiated in a method for Class and with the given Self
162 /// \param Method - The method being called, this may be null if synthesizing
163 /// a property setter or getter.
164 virtual CodeGen::RValue
165 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
166 ReturnValueSlot ReturnSlot,
169 const ObjCInterfaceDecl *Class,
173 const CallArgList &CallArgs,
174 const ObjCMethodDecl *Method = 0) = 0;
176 /// Emit the code to return the named protocol as an object, as in a
177 /// @protocol expression.
178 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
179 const ObjCProtocolDecl *OPD) = 0;
181 /// Generate the named protocol. Protocols contain method metadata but no
183 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
185 /// Generate a function preamble for a method with the specified
188 // FIXME: Current this just generates the Function definition, but really this
189 // should also be generating the loads of the parameters, as the runtime
190 // should have full control over how parameters are passed.
191 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
192 const ObjCContainerDecl *CD) = 0;
194 /// Return the runtime function for getting properties.
195 virtual llvm::Constant *GetPropertyGetFunction() = 0;
197 /// Return the runtime function for setting properties.
198 virtual llvm::Constant *GetPropertySetFunction() = 0;
200 // API for atomic copying of qualified aggregates in getter.
201 virtual llvm::Constant *GetGetStructFunction() = 0;
202 // API for atomic copying of qualified aggregates in setter.
203 virtual llvm::Constant *GetSetStructFunction() = 0;
205 /// GetClass - Return a reference to the class for the given
207 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
208 const ObjCInterfaceDecl *OID) = 0;
210 /// EnumerationMutationFunction - Return the function that's called by the
211 /// compiler when a mutation is detected during foreach iteration.
212 virtual llvm::Constant *EnumerationMutationFunction() = 0;
214 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
215 const ObjCAtSynchronizedStmt &S) = 0;
216 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
217 const ObjCAtTryStmt &S) = 0;
218 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
219 const ObjCAtThrowStmt &S) = 0;
220 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
221 llvm::Value *AddrWeakObj) = 0;
222 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
223 llvm::Value *src, llvm::Value *dest) = 0;
224 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
225 llvm::Value *src, llvm::Value *dest,
226 bool threadlocal=false) = 0;
227 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
228 llvm::Value *src, llvm::Value *dest,
229 llvm::Value *ivarOffset) = 0;
230 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
231 llvm::Value *src, llvm::Value *dest) = 0;
233 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
235 llvm::Value *BaseValue,
236 const ObjCIvarDecl *Ivar,
237 unsigned CVRQualifiers) = 0;
238 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
239 const ObjCInterfaceDecl *Interface,
240 const ObjCIvarDecl *Ivar) = 0;
241 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
242 llvm::Value *DestPtr,
244 llvm::Value *Size) = 0;
245 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
246 const CodeGen::CGBlockInfo &blockInfo) = 0;
249 /// Creates an instance of an Objective-C runtime class.
250 //TODO: This should include some way of selecting which runtime to target.
251 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
252 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);