CGObjCRuntime.h revision 60be607e5d71627bf3dab8f51c3fdca74267c692
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
26namespace llvm {
27  class Constant;
28  class Function;
29  class Module;
30  class StructLayout;
31  class StructType;
32  class Type;
33  class Value;
34}
35
36namespace clang {
37namespace 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
57namespace 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.
65class CGObjCRuntime {
66protected:
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
91public:
92  virtual ~CGObjCRuntime();
93
94  /// Generate the function required to register all Objective-C components in
95  /// this compilation unit with the runtime library.
96  virtual llvm::Function *ModuleInitFunction() = 0;
97
98  /// Get a selector for the specified name and type values. The
99  /// return value should have the LLVM type for pointer-to
100  /// ASTContext::getObjCSelType().
101  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
102                                   Selector Sel, bool lval=false) = 0;
103
104  /// Get a typed selector.
105  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
106                                   const ObjCMethodDecl *Method) = 0;
107
108  /// Get the type constant to catch for the given ObjC pointer type.
109  /// This is used externally to implement catching ObjC types in C++.
110  /// Runtimes which don't support this should add the appropriate
111  /// error to Sema.
112  virtual llvm::Constant *GetEHType(QualType T) = 0;
113
114  /// Generate a constant string object.
115  virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
116
117  /// Generate a category.  A category contains a list of methods (and
118  /// accompanying metadata) and a list of protocols.
119  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
120
121  /// Generate a class stucture for this class.
122  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
123
124  /// Generate an Objective-C message send operation.
125  ///
126  /// \param Method - The method being called, this may be null if synthesizing
127  /// a property setter or getter.
128  virtual CodeGen::RValue
129  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
130                      ReturnValueSlot ReturnSlot,
131                      QualType ResultType,
132                      Selector Sel,
133                      llvm::Value *Receiver,
134                      const CallArgList &CallArgs,
135                      const ObjCInterfaceDecl *Class = 0,
136                      const ObjCMethodDecl *Method = 0) = 0;
137
138  /// Generate an Objective-C message send operation to the super
139  /// class initiated in a method for Class and with the given Self
140  /// object.
141  ///
142  /// \param Method - The method being called, this may be null if synthesizing
143  /// a property setter or getter.
144  virtual CodeGen::RValue
145  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
146                           ReturnValueSlot ReturnSlot,
147                           QualType ResultType,
148                           Selector Sel,
149                           const ObjCInterfaceDecl *Class,
150                           bool isCategoryImpl,
151                           llvm::Value *Self,
152                           bool IsClassMessage,
153                           const CallArgList &CallArgs,
154                           const ObjCMethodDecl *Method = 0) = 0;
155
156  /// Emit the code to return the named protocol as an object, as in a
157  /// @protocol expression.
158  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
159                                           const ObjCProtocolDecl *OPD) = 0;
160
161  /// Generate the named protocol.  Protocols contain method metadata but no
162  /// implementations.
163  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
164
165  /// Generate a function preamble for a method with the specified
166  /// types.
167
168  // FIXME: Current this just generates the Function definition, but really this
169  // should also be generating the loads of the parameters, as the runtime
170  // should have full control over how parameters are passed.
171  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
172                                         const ObjCContainerDecl *CD) = 0;
173
174  /// Return the runtime function for getting properties.
175  virtual llvm::Constant *GetPropertyGetFunction() = 0;
176
177  /// Return the runtime function for setting properties.
178  virtual llvm::Constant *GetPropertySetFunction() = 0;
179
180  // API for atomic copying of qualified aggregates in getter.
181  virtual llvm::Constant *GetGetStructFunction() = 0;
182  // API for atomic copying of qualified aggregates in setter.
183  virtual llvm::Constant *GetSetStructFunction() = 0;
184
185  /// GetClass - Return a reference to the class for the given
186  /// interface decl.
187  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
188                                const ObjCInterfaceDecl *OID) = 0;
189
190  /// EnumerationMutationFunction - Return the function that's called by the
191  /// compiler when a mutation is detected during foreach iteration.
192  virtual llvm::Constant *EnumerationMutationFunction() = 0;
193
194  virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
195                                    const ObjCAtSynchronizedStmt &S) = 0;
196  virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
197                           const ObjCAtTryStmt &S) = 0;
198  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
199                             const ObjCAtThrowStmt &S) = 0;
200  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
201                                        llvm::Value *AddrWeakObj) = 0;
202  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
203                                  llvm::Value *src, llvm::Value *dest) = 0;
204  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
205                                    llvm::Value *src, llvm::Value *dest,
206                                    bool threadlocal=false) = 0;
207  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
208                                  llvm::Value *src, llvm::Value *dest,
209                                  llvm::Value *ivarOffset) = 0;
210  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
211                                        llvm::Value *src, llvm::Value *dest) = 0;
212
213  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
214                                      QualType ObjectTy,
215                                      llvm::Value *BaseValue,
216                                      const ObjCIvarDecl *Ivar,
217                                      unsigned CVRQualifiers) = 0;
218  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
219                                      const ObjCInterfaceDecl *Interface,
220                                      const ObjCIvarDecl *Ivar) = 0;
221  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
222                                        llvm::Value *DestPtr,
223                                        llvm::Value *SrcPtr,
224                                        llvm::Value *Size) = 0;
225  virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
226                                  const CodeGen::CGBlockInfo &blockInfo) = 0;
227};
228
229/// Creates an instance of an Objective-C runtime class.
230//TODO: This should include some way of selecting which runtime to target.
231CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
232CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
233}
234}
235#endif
236