]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGObjCRuntime.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / CodeGen / CGObjCRuntime.h
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.
64 class CGObjCRuntime {
65 protected:
66   CodeGen::CodeGenModule &CGM;
67   CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {}
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,
91                                   llvm::Value *Offset);
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
237   virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
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
282   /// Returns an i8* which points to the byref layout information.
283   virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
284                                            QualType T) = 0;
285
286   struct MessageSendInfo {
287     const CGFunctionInfo &CallInfo;
288     llvm::PointerType *MessengerType;
289
290     MessageSendInfo(const CGFunctionInfo &callInfo,
291                     llvm::PointerType *messengerType)
292       : CallInfo(callInfo), MessengerType(messengerType) {}
293   };
294
295   MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
296                                      QualType resultType,
297                                      CallArgList &callArgs);
298
299   // FIXME: This probably shouldn't be here, but the code to compute
300   // it is here.
301   unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,
302                                     const ObjCInterfaceDecl *ID,
303                                     const ObjCIvarDecl *Ivar);
304 };
305
306 /// Creates an instance of an Objective-C runtime class.
307 //TODO: This should include some way of selecting which runtime to target.
308 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
309 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
310 }
311 }
312 #endif