clang  3.7.0
CGObjCRuntime.h
Go to the documentation of this file.
1 //===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This provides an abstract class for Objective-C code generation. Concrete
11 // subclasses of this implement code generation for specific Objective-C
12 // runtime libraries.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
17 #define LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
18 #include "CGBuilder.h"
19 #include "CGCall.h"
20 #include "CGValue.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/Basic/IdentifierTable.h" // Selector
23 
24 namespace llvm {
25  class Constant;
26  class Function;
27  class Module;
28  class StructLayout;
29  class StructType;
30  class Type;
31  class Value;
32 }
33 
34 namespace clang {
35 namespace CodeGen {
36  class CodeGenFunction;
37 }
38 
39  class FieldDecl;
40  class ObjCAtTryStmt;
41  class ObjCAtThrowStmt;
42  class ObjCAtSynchronizedStmt;
43  class ObjCContainerDecl;
44  class ObjCCategoryImplDecl;
45  class ObjCImplementationDecl;
46  class ObjCInterfaceDecl;
47  class ObjCMessageExpr;
48  class ObjCMethodDecl;
49  class ObjCProtocolDecl;
50  class Selector;
51  class ObjCIvarDecl;
52  class ObjCStringLiteral;
53  class BlockDeclRefExpr;
54 
55 namespace CodeGen {
56  class CodeGenModule;
57  class CGBlockInfo;
58 
59 // FIXME: Several methods should be pure virtual but aren't to avoid the
60 // partially-implemented subclass breaking.
61 
62 /// Implements runtime-specific code generation functions.
64 protected:
67 
68  // Utility functions for unified ivar access. These need to
69  // eventually be folded into other places (the structure layout
70  // code).
71 
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!
75  ///
76  /// The latter overload is suitable for computing the offset of a
77  /// sythesized ivar.
79  const ObjCInterfaceDecl *OID,
80  const ObjCIvarDecl *Ivar);
82  const ObjCImplementationDecl *OID,
83  const ObjCIvarDecl *Ivar);
84 
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.
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.
108  const ObjCAtSynchronizedStmt &S,
109  llvm::Function *syncEnterFn,
110  llvm::Function *syncExitFn);
111 
112 public:
113  virtual ~CGObjCRuntime();
114 
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;
118 
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().
123  Selector Sel, bool lval=false) = 0;
124 
125  /// Get a typed selector.
127  const ObjCMethodDecl *Method) = 0;
128 
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
132  /// error to Sema.
133  virtual llvm::Constant *GetEHType(QualType T) = 0;
134 
135  /// Generate a constant string object.
136  virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
137 
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;
141 
142  /// Generate a class structure for this class.
143  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
144 
145  /// Register an class alias.
146  virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
147 
148  /// Generate an Objective-C message send operation.
149  ///
150  /// \param Method - The method being called, this may be null if synthesizing
151  /// a property setter or getter.
152  virtual CodeGen::RValue
154  ReturnValueSlot ReturnSlot,
155  QualType ResultType,
156  Selector Sel,
157  llvm::Value *Receiver,
158  const CallArgList &CallArgs,
159  const ObjCInterfaceDecl *Class = nullptr,
160  const ObjCMethodDecl *Method = nullptr) = 0;
161 
162  /// Generate an Objective-C message send operation to the super
163  /// class initiated in a method for Class and with the given Self
164  /// object.
165  ///
166  /// \param Method - The method being called, this may be null if synthesizing
167  /// a property setter or getter.
168  virtual CodeGen::RValue
170  ReturnValueSlot ReturnSlot,
171  QualType ResultType,
172  Selector Sel,
173  const ObjCInterfaceDecl *Class,
174  bool isCategoryImpl,
175  llvm::Value *Self,
176  bool IsClassMessage,
177  const CallArgList &CallArgs,
178  const ObjCMethodDecl *Method = nullptr) = 0;
179 
180  /// Emit the code to return the named protocol as an object, as in a
181  /// \@protocol expression.
183  const ObjCProtocolDecl *OPD) = 0;
184 
185  /// Generate the named protocol. Protocols contain method metadata but no
186  /// implementations.
187  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
188 
189  /// Generate a function preamble for a method with the specified
190  /// types.
191 
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;
197 
198  /// Return the runtime function for getting properties.
199  virtual llvm::Constant *GetPropertyGetFunction() = 0;
200 
201  /// Return the runtime function for setting properties.
202  virtual llvm::Constant *GetPropertySetFunction() = 0;
203 
204  /// Return the runtime function for optimized setting properties.
205  virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
206  bool copy) = 0;
207 
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;
218 
219  /// GetClass - Return a reference to the class for the given
220  /// interface decl.
221  virtual llvm::Value *GetClass(CodeGenFunction &CGF,
222  const ObjCInterfaceDecl *OID) = 0;
223 
224 
226  llvm_unreachable("autoreleasepool unsupported in this ABI");
227  }
228 
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;
232 
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;
241  llvm::Value *AddrWeakObj) = 0;
243  llvm::Value *src, llvm::Value *dest) = 0;
245  llvm::Value *src, llvm::Value *dest,
246  bool threadlocal=false) = 0;
248  llvm::Value *src, llvm::Value *dest,
249  llvm::Value *ivarOffset) = 0;
251  llvm::Value *src, llvm::Value *dest) = 0;
252 
254  QualType ObjectTy,
255  llvm::Value *BaseValue,
256  const ObjCIvarDecl *Ivar,
257  unsigned CVRQualifiers) = 0;
259  const ObjCInterfaceDecl *Interface,
260  const ObjCIvarDecl *Ivar) = 0;
262  llvm::Value *DestPtr,
263  llvm::Value *SrcPtr,
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,
270  QualType T) = 0;
271  virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name,
272  bool Weak = false) = 0;
273 
276  llvm::PointerType *MessengerType;
277 
279  llvm::PointerType *messengerType)
280  : CallInfo(callInfo), MessengerType(messengerType) {}
281  };
282 
283  MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
284  QualType resultType,
285  CallArgList &callArgs);
286 
287  // FIXME: This probably shouldn't be here, but the code to compute
288  // it is here.
290  const ObjCInterfaceDecl *ID,
291  const ObjCIvarDecl *Ivar);
292 };
293 
294 /// Creates an instance of an Objective-C runtime class.
295 //TODO: This should include some way of selecting which runtime to target.
296 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
297 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
298 }
299 }
300 #endif
Smart pointer class that efficiently represents Objective-C method names.
virtual llvm::Function * GenerateMethod(const ObjCMethodDecl *OMD, const ObjCContainerDecl *CD)=0
virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, ReturnValueSlot ReturnSlot, QualType ResultType, Selector Sel, llvm::Value *Receiver, const CallArgList &CallArgs, const ObjCInterfaceDecl *Class=nullptr, const ObjCMethodDecl *Method=nullptr)=0
Implements runtime-specific code generation functions.
Definition: CGObjCRuntime.h:63
virtual llvm::GlobalVariable * GetClassGlobal(const std::string &Name, bool Weak=false)=0
Represents Objective-C's @throw statement.
Definition: StmtObjC.h:313
virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, llvm::Value *dest)=0
uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, const ObjCInterfaceDecl *OID, const ObjCIvarDecl *Ivar)
virtual llvm::Constant * BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, const CodeGen::CGBlockInfo &blockInfo)=0
virtual llvm::Constant * GetOptimizedPropertySetFunction(bool atomic, bool copy)=0
Return the runtime function for optimized setting properties.
virtual llvm::Constant * GetPropertySetFunction()=0
Return the runtime function for setting properties.
virtual CodeGen::RValue GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, ReturnValueSlot ReturnSlot, QualType ResultType, Selector Sel, const ObjCInterfaceDecl *Class, bool isCategoryImpl, llvm::Value *Self, bool IsClassMessage, const CallArgList &CallArgs, const ObjCMethodDecl *Method=nullptr)=0
virtual llvm::Value * EmitIvarOffset(CodeGen::CodeGenFunction &CGF, const ObjCInterfaceDecl *Interface, const ObjCIvarDecl *Ivar)=0
MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method, QualType resultType, CallArgList &callArgs)
virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, llvm::Value *dest, bool threadlocal=false)=0
uint32_t Offset
Definition: CacheTokens.cpp:43
CGObjCRuntime * CreateMacObjCRuntime(CodeGenModule &CGM)
CodeGen::CodeGenModule & CGM
Definition: CGObjCRuntime.h:65
virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, llvm::Value *DestPtr, llvm::Value *SrcPtr, llvm::Value *Size)=0
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:1731
virtual llvm::Constant * GetGetStructFunction()=0
Represents an ObjC class declaration.
Definition: DeclObjC.h:851
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:144
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
virtual llvm::Constant * GetSetStructFunction()=0
CGObjCRuntime(CodeGen::CodeGenModule &CGM)
Definition: CGObjCRuntime.h:66
virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, const ObjCAtThrowStmt &S, bool ClearInsertionPoint=true)=0
virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD)=0
Register an class alias.
Represents Objective-C's @synchronized statement.
Definition: StmtObjC.h:262
virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, const ObjCAtTryStmt &S)=0
void EmitAtSynchronizedStmt(CodeGenFunction &CGF, const ObjCAtSynchronizedStmt &S, llvm::Function *syncEnterFn, llvm::Function *syncExitFn)
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
virtual llvm::Constant * GetPropertyGetFunction()=0
Return the runtime function for getting properties.
unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM, const ObjCInterfaceDecl *ID, const ObjCIvarDecl *Ivar)
virtual llvm::Constant * BuildByrefLayout(CodeGen::CodeGenModule &CGM, QualType T)=0
virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const ObjCAtSynchronizedStmt &S)=0
virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, QualType ObjectTy, llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, unsigned CVRQualifiers)=0
virtual void GenerateProtocol(const ObjCProtocolDecl *OPD)=0
MessageSendInfo(const CGFunctionInfo &callInfo, llvm::PointerType *messengerType)
virtual llvm::Value * EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF)
virtual llvm::Value * GenerateProtocolRef(CodeGenFunction &CGF, const ObjCProtocolDecl *OPD)=0
virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, llvm::Value *dest, llvm::Value *ivarOffset)=0
virtual llvm::Constant * GetCppAtomicObjectGetFunction()=0
virtual void GenerateClass(const ObjCImplementationDecl *OID)=0
Generate a class structure for this class.
virtual llvm::Constant * BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, const CodeGen::CGBlockInfo &blockInfo)=0
virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, llvm::Value *dest)=0
virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD)=0
virtual llvm::Function * ModuleInitFunction()=0
virtual llvm::Constant * EnumerationMutationFunction()=0
virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, llvm::Value *AddrWeakObj)=0
virtual llvm::Constant * GenerateConstantString(const StringLiteral *)=0
Generate a constant string object.
virtual llvm::Value * GetClass(CodeGenFunction &CGF, const ObjCInterfaceDecl *OID)=0
Represents Objective-C's @try ... @catch ... @finally statement.
Definition: StmtObjC.h:154
virtual llvm::Value * GetSelector(CodeGenFunction &CGF, Selector Sel, bool lval=false)=0
virtual llvm::Constant * GetCppAtomicObjectSetFunction()=0
void EmitTryCatchStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S, llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn, llvm::Constant *exceptionRethrowFn)
LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, const ObjCInterfaceDecl *OID, llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, unsigned CVRQualifiers, llvm::Value *Offset)
virtual llvm::Constant * GetEHType(QualType T)=0
CGObjCRuntime * CreateGNUObjCRuntime(CodeGenModule &CGM)
Creates an instance of an Objective-C runtime class.
Definition: CGObjCGNU.cpp:2834