clang  8.0.0svn
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 "CGCleanup.h"
21 #include "CGValue.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/Basic/IdentifierTable.h" // Selector
24 
25 namespace llvm {
26  class Constant;
27  class Function;
28  class Module;
29  class StructLayout;
30  class StructType;
31  class Type;
32  class Value;
33 }
34 
35 namespace clang {
36 namespace CodeGen {
37  class CodeGenFunction;
38 }
39 
40  class FieldDecl;
41  class ObjCAtTryStmt;
42  class ObjCAtThrowStmt;
43  class ObjCAtSynchronizedStmt;
44  class ObjCContainerDecl;
45  class ObjCCategoryImplDecl;
46  class ObjCImplementationDecl;
47  class ObjCInterfaceDecl;
48  class ObjCMessageExpr;
49  class ObjCMethodDecl;
50  class ObjCProtocolDecl;
51  class Selector;
52  class ObjCIvarDecl;
53  class ObjCStringLiteral;
54  class BlockDeclRefExpr;
55 
56 namespace CodeGen {
57  class CodeGenModule;
58  class CGBlockInfo;
59 
60 // FIXME: Several methods should be pure virtual but aren't to avoid the
61 // partially-implemented subclass breaking.
62 
63 /// Implements runtime-specific code generation functions.
65 protected:
68 
69  // Utility functions for unified ivar access. These need to
70  // eventually be folded into other places (the structure layout
71  // code).
72 
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!
76  ///
77  /// The latter overload is suitable for computing the offset of a
78  /// sythesized ivar.
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);
85 
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
95  /// to call when entering and exiting a \@catch() block, and the function
96  /// used to rethrow exceptions. If the begin and end catch functions are
97  /// NULL, then the function assumes that the EH personality function provides
98  /// the 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 
105  void EmitInitOfCatchParam(CodeGenFunction &CGF, llvm::Value *exn,
106  const VarDecl *paramDecl);
107 
108  /// Emits an \@synchronize() statement, using the \p syncEnterFn and
109  /// \p syncExitFn arguments as the functions called to lock and unlock
110  /// the object. This function can be called by subclasses that use
111  /// zero-cost exception handling.
112  void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
113  const ObjCAtSynchronizedStmt &S,
114  llvm::Function *syncEnterFn,
115  llvm::Function *syncExitFn);
116 
117 public:
118  virtual ~CGObjCRuntime();
119 
120  /// Generate the function required to register all Objective-C components in
121  /// this compilation unit with the runtime library.
122  virtual llvm::Function *ModuleInitFunction() = 0;
123 
124  /// Get a selector for the specified name and type values.
125  /// The result should have the LLVM type for ASTContext::getObjCSelType().
126  virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) = 0;
127 
128  /// Get the address of a selector for the specified name and type values.
129  /// This is a rarely-used language extension, but sadly it exists.
130  ///
131  /// The result should have the LLVM type for a pointer to
132  /// ASTContext::getObjCSelType().
133  virtual Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) = 0;
134 
135  /// Get a typed selector.
136  virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
137  const ObjCMethodDecl *Method) = 0;
138 
139  /// Get the type constant to catch for the given ObjC pointer type.
140  /// This is used externally to implement catching ObjC types in C++.
141  /// Runtimes which don't support this should add the appropriate
142  /// error to Sema.
143  virtual llvm::Constant *GetEHType(QualType T) = 0;
144 
145  virtual CatchTypeInfo getCatchAllTypeInfo() { return { nullptr, 0 }; }
146 
147  /// Generate a constant string object.
148  virtual ConstantAddress GenerateConstantString(const StringLiteral *) = 0;
149 
150  /// Generate a category. A category contains a list of methods (and
151  /// accompanying metadata) and a list of protocols.
152  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
153 
154  /// Generate a class structure for this class.
155  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
156 
157  /// Register an class alias.
158  virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
159 
160  /// Generate an Objective-C message send operation.
161  ///
162  /// \param Method - The method being called, this may be null if synthesizing
163  /// a property setter or getter.
164  virtual CodeGen::RValue
165  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
166  ReturnValueSlot ReturnSlot,
167  QualType ResultType,
168  Selector Sel,
169  llvm::Value *Receiver,
170  const CallArgList &CallArgs,
171  const ObjCInterfaceDecl *Class = nullptr,
172  const ObjCMethodDecl *Method = nullptr) = 0;
173 
174  /// Generate an Objective-C message send operation to the super
175  /// class initiated in a method for Class and with the given Self
176  /// object.
177  ///
178  /// \param Method - The method being called, this may be null if synthesizing
179  /// a property setter or getter.
180  virtual CodeGen::RValue
181  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
182  ReturnValueSlot ReturnSlot,
183  QualType ResultType,
184  Selector Sel,
185  const ObjCInterfaceDecl *Class,
186  bool isCategoryImpl,
187  llvm::Value *Self,
188  bool IsClassMessage,
189  const CallArgList &CallArgs,
190  const ObjCMethodDecl *Method = nullptr) = 0;
191 
192  /// Emit the code to return the named protocol as an object, as in a
193  /// \@protocol expression.
194  virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
195  const ObjCProtocolDecl *OPD) = 0;
196 
197  /// Generate the named protocol. Protocols contain method metadata but no
198  /// implementations.
199  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
200 
201  /// Generate a function preamble for a method with the specified
202  /// types.
203 
204  // FIXME: Current this just generates the Function definition, but really this
205  // should also be generating the loads of the parameters, as the runtime
206  // should have full control over how parameters are passed.
207  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
208  const ObjCContainerDecl *CD) = 0;
209 
210  /// Return the runtime function for getting properties.
211  virtual llvm::Constant *GetPropertyGetFunction() = 0;
212 
213  /// Return the runtime function for setting properties.
214  virtual llvm::Constant *GetPropertySetFunction() = 0;
215 
216  /// Return the runtime function for optimized setting properties.
217  virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
218  bool copy) = 0;
219 
220  // API for atomic copying of qualified aggregates in getter.
221  virtual llvm::Constant *GetGetStructFunction() = 0;
222  // API for atomic copying of qualified aggregates in setter.
223  virtual llvm::Constant *GetSetStructFunction() = 0;
224  /// API for atomic copying of qualified aggregates with non-trivial copy
225  /// assignment (c++) in setter.
226  virtual llvm::Constant *GetCppAtomicObjectSetFunction() = 0;
227  /// API for atomic copying of qualified aggregates with non-trivial copy
228  /// assignment (c++) in getter.
229  virtual llvm::Constant *GetCppAtomicObjectGetFunction() = 0;
230 
231  /// GetClass - Return a reference to the class for the given
232  /// interface decl.
233  virtual llvm::Value *GetClass(CodeGenFunction &CGF,
234  const ObjCInterfaceDecl *OID) = 0;
235 
236 
238  llvm_unreachable("autoreleasepool unsupported in this ABI");
239  }
240 
241  /// EnumerationMutationFunction - Return the function that's called by the
242  /// compiler when a mutation is detected during foreach iteration.
243  virtual llvm::Constant *EnumerationMutationFunction() = 0;
244 
245  virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
246  const ObjCAtSynchronizedStmt &S) = 0;
247  virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
248  const ObjCAtTryStmt &S) = 0;
249  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
250  const ObjCAtThrowStmt &S,
251  bool ClearInsertionPoint=true) = 0;
252  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
253  Address AddrWeakObj) = 0;
254  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
255  llvm::Value *src, Address dest) = 0;
256  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
257  llvm::Value *src, Address dest,
258  bool threadlocal=false) = 0;
259  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
260  llvm::Value *src, Address dest,
261  llvm::Value *ivarOffset) = 0;
262  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
263  llvm::Value *src, Address dest) = 0;
264 
265  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
266  QualType ObjectTy,
267  llvm::Value *BaseValue,
268  const ObjCIvarDecl *Ivar,
269  unsigned CVRQualifiers) = 0;
270  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
271  const ObjCInterfaceDecl *Interface,
272  const ObjCIvarDecl *Ivar) = 0;
273  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
274  Address DestPtr,
275  Address SrcPtr,
276  llvm::Value *Size) = 0;
277  virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
278  const CodeGen::CGBlockInfo &blockInfo) = 0;
279  virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
280  const CodeGen::CGBlockInfo &blockInfo) = 0;
281  virtual std::string getRCBlockLayoutStr(CodeGen::CodeGenModule &CGM,
282  const CGBlockInfo &blockInfo) {
283  return {};
284  }
285 
286  /// Returns an i8* which points to the byref layout information.
287  virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
288  QualType T) = 0;
289 
292  llvm::PointerType *MessengerType;
293 
295  llvm::PointerType *messengerType)
296  : CallInfo(callInfo), MessengerType(messengerType) {}
297  };
298 
299  MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
300  QualType resultType,
301  CallArgList &callArgs);
302 
303  // FIXME: This probably shouldn't be here, but the code to compute
304  // it is here.
305  unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,
306  const ObjCInterfaceDecl *ID,
307  const ObjCIvarDecl *Ivar);
308 };
309 
310 /// Creates an instance of an Objective-C runtime class.
311 //TODO: This should include some way of selecting which runtime to target.
314 }
315 }
316 #endif
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:361
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:642
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
Implements runtime-specific code generation functions.
Definition: CGObjCRuntime.h:64
Represents Objective-C's @throw statement.
Definition: StmtObjC.h:313
constexpr XRayInstrMask Function
Definition: XRayInstr.h:39
Represents a variable declaration or definition.
Definition: Decl.h:812
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
virtual std::string getRCBlockLayoutStr(CodeGen::CodeGenModule &CGM, const CGBlockInfo &blockInfo)
ObjCContainerDecl - Represents a container for method declarations.
Definition: DeclObjC.h:961
uint32_t Offset
Definition: CacheTokens.cpp:43
CGObjCRuntime * CreateMacObjCRuntime(CodeGenModule &CGM)
CodeGen::CodeGenModule & CGM
Definition: CGObjCRuntime.h:66
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2056
Represents an ObjC class declaration.
Definition: DeclObjC.h:1164
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:153
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:39
CGObjCRuntime(CodeGen::CodeGenModule &CGM)
Definition: CGObjCRuntime.h:67
Represents Objective-C's @synchronized statement.
Definition: StmtObjC.h:262
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
virtual CatchTypeInfo getCatchAllTypeInfo()
MessageSendInfo(const CGFunctionInfo &callInfo, llvm::PointerType *messengerType)
virtual llvm::Value * EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF)
An aligned address.
Definition: Address.h:25
The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the type of a catch handler...
Definition: CGCleanup.h:38
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
Optional< types::ID > Type
Dataflow Directional Tag Classes.
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Definition: DeclObjC.h:2544
A specialization of Address that requires the address to be an LLVM Constant.
Definition: Address.h:75
ObjCIvarDecl - Represents an ObjC instance variable.
Definition: DeclObjC.h:1937
Represents Objective-C&#39;s @try ... @catch ... @finally statement.
Definition: StmtObjC.h:154
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1568
LValue - This represents an lvalue references.
Definition: CGValue.h:167
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:260
ObjCCategoryImplDecl - An object of this class encapsulates a category @implementation declaration...
Definition: DeclObjC.h:2491
ObjCCompatibleAliasDecl - Represents alias of a class.
Definition: DeclObjC.h:2721
CGObjCRuntime * CreateGNUObjCRuntime(CodeGenModule &CGM)
Creates an instance of an Objective-C runtime class.
Definition: CGObjCGNU.cpp:3987