CGObjCRuntime.h revision a8fa96e366ab36145a5500dd4fbea717c217f131
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 "llvm/ADT/SmallVector.h"
20#include "clang/AST/DeclObjC.h"
21#include <string>
22
23#include "CGBuilder.h"
24#include "CGCall.h"
25#include "CGValue.h"
26
27namespace llvm {
28  class Constant;
29  class Function;
30  class Module;
31  class StructLayout;
32  class StructType;
33  class Type;
34  class Value;
35}
36
37namespace clang {
38namespace CodeGen {
39  class CodeGenFunction;
40}
41
42  class FieldDecl;
43  class ObjCAtTryStmt;
44  class ObjCAtThrowStmt;
45  class ObjCAtSynchronizedStmt;
46  class ObjCContainerDecl;
47  class ObjCCategoryImplDecl;
48  class ObjCImplementationDecl;
49  class ObjCInterfaceDecl;
50  class ObjCMessageExpr;
51  class ObjCMethodDecl;
52  class ObjCProtocolDecl;
53  class Selector;
54  class ObjCIvarDecl;
55  class ObjCStringLiteral;
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 {
65public:
66  // Utility functions for unified ivar access. These need to
67  // eventually be folded into other places (the structure layout
68  // code).
69
70protected:
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  virtual llvm::Constant *GetConstantSelector(Selector Sel) = 0;
99
100  /// Get a typed selector.
101  virtual llvm::Constant *GetConstantTypedSelector(
102     const ObjCMethodDecl *Method) = 0;
103
104  /// Get a selector for the specified name and type values. The
105  /// return value should have the LLVM type for pointer-to
106  /// ASTContext::getObjCSelType().
107  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
108                                   Selector Sel) = 0;
109
110  /// Get a typed selector.
111  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
112                                   const ObjCMethodDecl *Method) = 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                      QualType ResultType,
131                      Selector Sel,
132                      llvm::Value *Receiver,
133                      bool IsClassMessage,
134                      const CallArgList &CallArgs,
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                           QualType ResultType,
146                           Selector Sel,
147                           const ObjCInterfaceDecl *Class,
148                           bool isCategoryImpl,
149                           llvm::Value *Self,
150                           bool IsClassMessage,
151                           const CallArgList &CallArgs,
152                           const ObjCMethodDecl *Method = 0) = 0;
153
154  /// Emit the code to return the named protocol as an object, as in a
155  /// @protocol expression.
156  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
157                                           const ObjCProtocolDecl *OPD) = 0;
158
159  /// Generate the named protocol.  Protocols contain method metadata but no
160  /// implementations.
161  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
162
163  /// Generate a function preamble for a method with the specified
164  /// types.
165
166  // FIXME: Current this just generates the Function definition, but really this
167  // should also be generating the loads of the parameters, as the runtime
168  // should have full control over how parameters are passed.
169  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
170                                         const ObjCContainerDecl *CD) = 0;
171
172  /// Return the runtime function for getting properties.
173  virtual llvm::Constant *GetPropertyGetFunction() = 0;
174
175  /// Return the runtime function for setting properties.
176  virtual llvm::Constant *GetPropertySetFunction() = 0;
177
178  /// GetClass - Return a reference to the class for the given
179  /// interface decl.
180  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
181                                const ObjCInterfaceDecl *OID) = 0;
182
183  /// EnumerationMutationFunction - Return the function that's called by the
184  /// compiler when a mutation is detected during foreach iteration.
185  virtual llvm::Constant *EnumerationMutationFunction() = 0;
186
187  virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
188                                         const Stmt &S) = 0;
189  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
190                             const ObjCAtThrowStmt &S) = 0;
191  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
192                                        llvm::Value *AddrWeakObj) = 0;
193  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
194                                  llvm::Value *src, llvm::Value *dest) = 0;
195  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
196                                    llvm::Value *src, llvm::Value *dest) = 0;
197  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
198                                  llvm::Value *src, llvm::Value *dest,
199                                  llvm::Value *ivarOffset) = 0;
200  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
201                                        llvm::Value *src, llvm::Value *dest) = 0;
202
203  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
204                                      QualType ObjectTy,
205                                      llvm::Value *BaseValue,
206                                      const ObjCIvarDecl *Ivar,
207                                      unsigned CVRQualifiers) = 0;
208  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
209                                      const ObjCInterfaceDecl *Interface,
210                                      const ObjCIvarDecl *Ivar) = 0;
211  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
212                                        llvm::Value *DestPtr,
213                                        llvm::Value *SrcPtr,
214                                        QualType Ty) = 0;
215};
216
217/// Creates an instance of an Objective-C runtime class.
218//TODO: This should include some way of selecting which runtime to target.
219CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
220CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
221CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
222}
223}
224#endif
225