CGObjCRuntime.h revision 206084
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 {
64public:
65  // Utility functions for unified ivar access. These need to
66  // eventually be folded into other places (the structure layout
67  // code).
68
69protected:
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) = 0;
102
103  /// Get a typed selector.
104  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
105                                   const ObjCMethodDecl *Method) = 0;
106
107  /// Generate a constant string object.
108  virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
109
110  /// Generate a category.  A category contains a list of methods (and
111  /// accompanying metadata) and a list of protocols.
112  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
113
114  /// Generate a class stucture for this class.
115  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
116
117  /// Generate an Objective-C message send operation.
118  ///
119  /// \param Method - The method being called, this may be null if synthesizing
120  /// a property setter or getter.
121  virtual CodeGen::RValue
122  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
123                      QualType ResultType,
124                      Selector Sel,
125                      llvm::Value *Receiver,
126                      bool IsClassMessage,
127                      const CallArgList &CallArgs,
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                           QualType ResultType,
139                           Selector Sel,
140                           const ObjCInterfaceDecl *Class,
141                           bool isCategoryImpl,
142                           llvm::Value *Self,
143                           bool IsClassMessage,
144                           const CallArgList &CallArgs,
145                           const ObjCMethodDecl *Method = 0) = 0;
146
147  /// Emit the code to return the named protocol as an object, as in a
148  /// @protocol expression.
149  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
150                                           const ObjCProtocolDecl *OPD) = 0;
151
152  /// Generate the named protocol.  Protocols contain method metadata but no
153  /// implementations.
154  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
155
156  /// Generate a function preamble for a method with the specified
157  /// types.
158
159  // FIXME: Current this just generates the Function definition, but really this
160  // should also be generating the loads of the parameters, as the runtime
161  // should have full control over how parameters are passed.
162  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
163                                         const ObjCContainerDecl *CD) = 0;
164
165  /// Return the runtime function for getting properties.
166  virtual llvm::Constant *GetPropertyGetFunction() = 0;
167
168  /// Return the runtime function for setting properties.
169  virtual llvm::Constant *GetPropertySetFunction() = 0;
170
171  /// GetClass - Return a reference to the class for the given
172  /// interface decl.
173  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
174                                const ObjCInterfaceDecl *OID) = 0;
175
176  /// EnumerationMutationFunction - Return the function that's called by the
177  /// compiler when a mutation is detected during foreach iteration.
178  virtual llvm::Constant *EnumerationMutationFunction() = 0;
179
180  virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
181                                         const Stmt &S) = 0;
182  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
183                             const ObjCAtThrowStmt &S) = 0;
184  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
185                                        llvm::Value *AddrWeakObj) = 0;
186  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
187                                  llvm::Value *src, llvm::Value *dest) = 0;
188  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
189                                    llvm::Value *src, llvm::Value *dest) = 0;
190  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
191                                  llvm::Value *src, llvm::Value *dest,
192                                  llvm::Value *ivarOffset) = 0;
193  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
194                                        llvm::Value *src, llvm::Value *dest) = 0;
195
196  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
197                                      QualType ObjectTy,
198                                      llvm::Value *BaseValue,
199                                      const ObjCIvarDecl *Ivar,
200                                      unsigned CVRQualifiers) = 0;
201  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
202                                      const ObjCInterfaceDecl *Interface,
203                                      const ObjCIvarDecl *Ivar) = 0;
204  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
205                                        llvm::Value *DestPtr,
206                                        llvm::Value *SrcPtr,
207                                        QualType Ty) = 0;
208};
209
210/// Creates an instance of an Objective-C runtime class.
211//TODO: This should include some way of selecting which runtime to target.
212CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
213CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
214CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
215}
216}
217#endif
218