]> granicus.if.org Git - clang/blob - lib/CodeGen/CGObjCRuntime.h
Continuing work on ObjC tidyup:
[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 CLANG_CODEGEN_OBCJRUNTIME_H
17 #define CLANG_CODEGEN_OBCJRUNTIME_H
18 #include "clang/Basic/IdentifierTable.h" // Selector
19 #include "clang/AST/DeclObjC.h"
20 #include <string>
21
22 #include "CGBuilder.h"
23 #include "CGCall.h"
24 #include "CGValue.h"
25
26 namespace llvm {
27   class Constant;
28   class Function;
29   class Module;
30   class StructLayout;
31   class StructType;
32   class Type;
33   class Value;
34 }
35
36 namespace clang {
37 namespace CodeGen {
38   class CodeGenFunction;
39 }
40
41   class FieldDecl;
42   class ObjCAtTryStmt;
43   class ObjCAtThrowStmt;
44   class ObjCAtSynchronizedStmt;
45   class ObjCContainerDecl;
46   class ObjCCategoryImplDecl;
47   class ObjCImplementationDecl;
48   class ObjCInterfaceDecl;
49   class ObjCMessageExpr;
50   class ObjCMethodDecl;
51   class ObjCProtocolDecl;
52   class Selector;
53   class ObjCIvarDecl;
54   class ObjCStringLiteral;
55   class BlockDeclRefExpr;
56
57 namespace CodeGen {
58   class CodeGenModule;
59   class CGBlockInfo;
60
61 // FIXME: Several methods should be pure virtual but aren't to avoid the
62 // partially-implemented subclass breaking.
63
64 /// Implements runtime-specific code generation functions.
65 class CGObjCRuntime {
66 protected:
67   // Utility functions for unified ivar access. These need to
68   // eventually be folded into other places (the structure layout
69   // code).
70
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!
74   ///
75   /// The latter overload is suitable for computing the offset of a
76   /// sythesized ivar.
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);
83
84   LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
85                                   const ObjCInterfaceDecl *OID,
86                                   llvm::Value *BaseValue,
87                                   const ObjCIvarDecl *Ivar,
88                                   unsigned CVRQualifiers,
89                                   llvm::Value *Offset);
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
105   /// handling.
106   void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
107                             const ObjCAtSynchronizedStmt &S,
108                             llvm::Function *syncEnterFn,
109                             llvm::Function *syncExitFn);
110
111 public:
112   virtual ~CGObjCRuntime();
113
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;
117
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;
123
124   /// Get a typed selector.
125   virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
126                                    const ObjCMethodDecl *Method) = 0;
127
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
131   /// error to Sema.
132   virtual llvm::Constant *GetEHType(QualType T) = 0;
133
134   /// Generate a constant string object.
135   virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
136
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;
140
141   /// Generate a class stucture for this class.
142   virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
143
144   /// Generate an Objective-C message send operation.
145   ///
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,
151                       QualType ResultType,
152                       Selector Sel,
153                       llvm::Value *Receiver,
154                       const CallArgList &CallArgs,
155                       const ObjCInterfaceDecl *Class = 0,
156                       const ObjCMethodDecl *Method = 0) = 0;
157
158   /// Generate an Objective-C message send operation to the super
159   /// class initiated in a method for Class and with the given Self
160   /// object.
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   GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
166                            ReturnValueSlot ReturnSlot,
167                            QualType ResultType,
168                            Selector Sel,
169                            const ObjCInterfaceDecl *Class,
170                            bool isCategoryImpl,
171                            llvm::Value *Self,
172                            bool IsClassMessage,
173                            const CallArgList &CallArgs,
174                            const ObjCMethodDecl *Method = 0) = 0;
175
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;
180
181   /// Generate the named protocol.  Protocols contain method metadata but no
182   /// implementations.
183   virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
184
185   /// Generate a function preamble for a method with the specified
186   /// types.
187
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;
193
194   /// Return the runtime function for getting properties.
195   virtual llvm::Constant *GetPropertyGetFunction() = 0;
196
197   /// Return the runtime function for setting properties.
198   virtual llvm::Constant *GetPropertySetFunction() = 0;
199
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;
204   
205   /// GetClass - Return a reference to the class for the given
206   /// interface decl.
207   virtual llvm::Value *GetClass(CGBuilderTy &Builder,
208                                 const ObjCInterfaceDecl *OID) = 0;
209
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;
213
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;
232
233   virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
234                                       QualType ObjectTy,
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,
243                                         llvm::Value *SrcPtr,
244                                         llvm::Value *Size) = 0;
245   virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
246                                   const CodeGen::CGBlockInfo &blockInfo) = 0;
247 };
248
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);
253 }
254 }
255 #endif