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