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