CGObjCRuntime.h revision 193326
1193323Sed//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This provides an abstract class for Objective-C code generation.  Concrete
11193323Sed// subclasses of this implement code generation for specific Objective-C
12193323Sed// runtime libraries.
13193323Sed//
14234353Sdim//===----------------------------------------------------------------------===//
15193323Sed
16193323Sed#ifndef CLANG_CODEGEN_OBCJRUNTIME_H
17218893Sdim#define CLANG_CODEGEN_OBCJRUNTIME_H
18234353Sdim#include "clang/Basic/IdentifierTable.h" // Selector
19193323Sed#include "llvm/ADT/SmallVector.h"
20239462Sdim#include "clang/AST/DeclObjC.h"
21198090Srdivacky#include <string>
22226633Sdim
23193323Sed#include "CGBuilder.h"
24193323Sed#include "CGCall.h"
25239462Sdim#include "CGValue.h"
26239462Sdim
27239462Sdimnamespace llvm {
28239462Sdim  class Constant;
29198090Srdivacky  class Function;
30198090Srdivacky  class Module;
31234353Sdim  class StructLayout;
32198090Srdivacky  class StructType;
33193323Sed  class Type;
34193323Sed  class Value;
35226633Sdim}
36226633Sdim
37234353Sdimnamespace clang {
38226633Sdimnamespace CodeGen {
39234353Sdim  class CodeGenFunction;
40226633Sdim}
41234353Sdim
42224145Sdim  class FieldDecl;
43193323Sed  class ObjCAtTryStmt;
44218893Sdim  class ObjCAtThrowStmt;
45208599Srdivacky  class ObjCAtSynchronizedStmt;
46203954Srdivacky  class ObjCContainerDecl;
47234353Sdim  class ObjCCategoryImplDecl;
48234353Sdim  class ObjCImplementationDecl;
49234353Sdim  class ObjCInterfaceDecl;
50234353Sdim  class ObjCMessageExpr;
51193323Sed  class ObjCMethodDecl;
52193323Sed  class ObjCProtocolDecl;
53234353Sdim  class Selector;
54193323Sed  class ObjCIvarDecl;
55234353Sdim  class ObjCStringLiteral;
56226633Sdim
57234353Sdimnamespace CodeGen {
58234353Sdim  class CodeGenModule;
59234353Sdim
60234353Sdim// FIXME: Several methods should be pure virtual but aren't to avoid the
61193323Sed// partially-implemented subclass breaking.
62193323Sed
63234353Sdim/// Implements runtime-specific code generation functions.
64193323Sedclass CGObjCRuntime {
65234353Sdimpublic:
66226633Sdim  // Utility functions for unified ivar access. These need to
67234353Sdim  // eventually be folded into other places (the structure layout
68234353Sdim  // code).
69234353Sdim
70234353Sdimprotected:
71193323Sed  /// Compute an offset to the given ivar, suitable for passing to
72193323Sed  /// EmitValueForIvarAtOffset.  Note that the correct handling of
73193323Sed  /// bit-fields is carefully coordinated by these two, use caution!
74193323Sed  ///
75193323Sed  /// The latter overload is suitable for computing the offset of a
76193323Sed  /// sythesized ivar.
77193323Sed  uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
78234353Sdim                                 const ObjCInterfaceDecl *OID,
79234353Sdim                                 const ObjCIvarDecl *Ivar);
80234353Sdim  uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
81234353Sdim                                 const ObjCImplementationDecl *OID,
82234353Sdim                                 const ObjCIvarDecl *Ivar);
83234353Sdim
84234353Sdim  LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
85234353Sdim                                  const ObjCInterfaceDecl *OID,
86234353Sdim                                  llvm::Value *BaseValue,
87234353Sdim                                  const ObjCIvarDecl *Ivar,
88234353Sdim                                  unsigned CVRQualifiers,
89239462Sdim                                  llvm::Value *Offset);
90234353Sdim
91234353Sdimpublic:
92234353Sdim  virtual ~CGObjCRuntime();
93234353Sdim
94234353Sdim  /// Generate the function required to register all Objective-C components in
95234353Sdim  /// this compilation unit with the runtime library.
96239462Sdim  virtual llvm::Function *ModuleInitFunction() = 0;
97239462Sdim
98234353Sdim  /// Get a selector for the specified name and type values. The
99239462Sdim  /// return value should have the LLVM type for pointer-to
100239462Sdim  /// ASTContext::getObjCSelType().
101239462Sdim  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
102234353Sdim                                   Selector Sel) = 0;
103239462Sdim
104234353Sdim  /// Get a typed selector.
105234353Sdim  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
106234353Sdim                                   const ObjCMethodDecl *Method) = 0;
107193323Sed
108239462Sdim  /// Generate a constant string object.
109193323Sed  virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0;
110193323Sed
111193323Sed  /// Generate a category.  A category contains a list of methods (and
112234353Sdim  /// accompanying metadata) and a list of protocols.
113193323Sed  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
114239462Sdim
115193323Sed  /// Generate a class stucture for this class.
116193323Sed  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
117193323Sed
118198090Srdivacky  /// Generate an Objective-C message send operation.
119198090Srdivacky  virtual CodeGen::RValue
120193323Sed  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
121193323Sed                      QualType ResultType,
122193323Sed                      Selector Sel,
123234353Sdim                      llvm::Value *Receiver,
124193323Sed                      bool IsClassMessage,
125198090Srdivacky                      const CallArgList &CallArgs,
126193323Sed                      const ObjCMethodDecl *Method=0) = 0;
127193323Sed
128193323Sed  /// Generate an Objective-C message send operation to the super
129  /// class initiated in a method for Class and with the given Self
130  /// object.
131  virtual CodeGen::RValue
132  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
133                           QualType ResultType,
134                           Selector Sel,
135                           const ObjCInterfaceDecl *Class,
136                           bool isCategoryImpl,
137                           llvm::Value *Self,
138                           bool IsClassMessage,
139                           const CallArgList &CallArgs) = 0;
140
141  /// Emit the code to return the named protocol as an object, as in a
142  /// @protocol expression.
143  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
144                                           const ObjCProtocolDecl *OPD) = 0;
145
146  /// Generate the named protocol.  Protocols contain method metadata but no
147  /// implementations.
148  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
149
150  /// Generate a function preamble for a method with the specified
151  /// types.
152
153  // FIXME: Current this just generates the Function definition, but really this
154  // should also be generating the loads of the parameters, as the runtime
155  // should have full control over how parameters are passed.
156  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
157                                         const ObjCContainerDecl *CD) = 0;
158
159  /// Return the runtime function for getting properties.
160  virtual llvm::Constant *GetPropertyGetFunction() = 0;
161
162  /// Return the runtime function for setting properties.
163  virtual llvm::Constant *GetPropertySetFunction() = 0;
164
165  /// GetClass - Return a reference to the class for the given
166  /// interface decl.
167  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
168                                const ObjCInterfaceDecl *OID) = 0;
169
170  /// EnumerationMutationFunction - Return the function that's called by the
171  /// compiler when a mutation is detected during foreach iteration.
172  virtual llvm::Constant *EnumerationMutationFunction() = 0;
173
174  virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
175                                         const Stmt &S) = 0;
176  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
177                             const ObjCAtThrowStmt &S) = 0;
178  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
179                                        llvm::Value *AddrWeakObj) = 0;
180  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
181                                  llvm::Value *src, llvm::Value *dest) = 0;
182  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
183                                    llvm::Value *src, llvm::Value *dest) = 0;
184  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
185                                  llvm::Value *src, llvm::Value *dest) = 0;
186  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
187                                        llvm::Value *src, llvm::Value *dest) = 0;
188
189  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
190                                      QualType ObjectTy,
191                                      llvm::Value *BaseValue,
192                                      const ObjCIvarDecl *Ivar,
193                                      unsigned CVRQualifiers) = 0;
194  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
195                                      const ObjCInterfaceDecl *Interface,
196                                      const ObjCIvarDecl *Ivar) = 0;
197};
198
199/// Creates an instance of an Objective-C runtime class.
200//TODO: This should include some way of selecting which runtime to target.
201CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
202CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
203CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
204}
205}
206#endif
207