CGObjCRuntime.h revision 210299
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, bool lval=false) = 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                      ReturnValueSlot ReturnSlot,
123                      QualType ResultType,
124                      Selector Sel,
125                      llvm::Value *Receiver,
126                      const CallArgList &CallArgs,
127                      const ObjCInterfaceDecl *Class = 0,
128                      const ObjCMethodDecl *Method = 0) = 0;
129
130  /// Generate an Objective-C message send operation to the super
131  /// class initiated in a method for Class and with the given Self
132  /// object.
133  ///
134  /// \param Method - The method being called, this may be null if synthesizing
135  /// a property setter or getter.
136  virtual CodeGen::RValue
137  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
138                           ReturnValueSlot ReturnSlot,
139                           QualType ResultType,
140                           Selector Sel,
141                           const ObjCInterfaceDecl *Class,
142                           bool isCategoryImpl,
143                           llvm::Value *Self,
144                           bool IsClassMessage,
145                           const CallArgList &CallArgs,
146                           const ObjCMethodDecl *Method = 0) = 0;
147
148  /// Emit the code to return the named protocol as an object, as in a
149  /// @protocol expression.
150  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
151                                           const ObjCProtocolDecl *OPD) = 0;
152
153  /// Generate the named protocol.  Protocols contain method metadata but no
154  /// implementations.
155  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
156
157  /// Generate a function preamble for a method with the specified
158  /// types.
159
160  // FIXME: Current this just generates the Function definition, but really this
161  // should also be generating the loads of the parameters, as the runtime
162  // should have full control over how parameters are passed.
163  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
164                                         const ObjCContainerDecl *CD) = 0;
165
166  /// Return the runtime function for getting properties.
167  virtual llvm::Constant *GetPropertyGetFunction() = 0;
168
169  /// Return the runtime function for setting properties.
170  virtual llvm::Constant *GetPropertySetFunction() = 0;
171
172  // API for atomic copying of qualified aggregates in setter/getter.
173  virtual llvm::Constant *GetCopyStructFunction() = 0;
174
175  /// GetClass - Return a reference to the class for the given
176  /// interface decl.
177  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
178                                const ObjCInterfaceDecl *OID) = 0;
179
180  /// EnumerationMutationFunction - Return the function that's called by the
181  /// compiler when a mutation is detected during foreach iteration.
182  virtual llvm::Constant *EnumerationMutationFunction() = 0;
183
184  virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
185                                    const ObjCAtSynchronizedStmt &S) = 0;
186  virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
187                           const ObjCAtTryStmt &S) = 0;
188  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
189                             const ObjCAtThrowStmt &S) = 0;
190  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
191                                        llvm::Value *AddrWeakObj) = 0;
192  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
193                                  llvm::Value *src, llvm::Value *dest) = 0;
194  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
195                                    llvm::Value *src, llvm::Value *dest) = 0;
196  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
197                                  llvm::Value *src, llvm::Value *dest,
198                                  llvm::Value *ivarOffset) = 0;
199  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
200                                        llvm::Value *src, llvm::Value *dest) = 0;
201
202  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
203                                      QualType ObjectTy,
204                                      llvm::Value *BaseValue,
205                                      const ObjCIvarDecl *Ivar,
206                                      unsigned CVRQualifiers) = 0;
207  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
208                                      const ObjCInterfaceDecl *Interface,
209                                      const ObjCIvarDecl *Ivar) = 0;
210  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
211                                        llvm::Value *DestPtr,
212                                        llvm::Value *SrcPtr,
213                                        llvm::Value *Size) = 0;
214};
215
216/// Creates an instance of an Objective-C runtime class.
217//TODO: This should include some way of selecting which runtime to target.
218CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
219CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
220CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
221}
222}
223#endif
224