CGObjCRuntime.h revision 221345
1254721Semaste//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste//
10254721Semaste// This provides an abstract class for Objective-C code generation.  Concrete
11254721Semaste// subclasses of this implement code generation for specific Objective-C
12254721Semaste// runtime libraries.
13254721Semaste//
14254721Semaste//===----------------------------------------------------------------------===//
15263367Semaste
16263367Semaste#ifndef CLANG_CODEGEN_OBCJRUNTIME_H
17263367Semaste#define CLANG_CODEGEN_OBCJRUNTIME_H
18254721Semaste#include "clang/Basic/IdentifierTable.h" // Selector
19254721Semaste#include "clang/AST/DeclObjC.h"
20269024Semaste
21269024Semaste#include "CGBuilder.h"
22269024Semaste#include "CGCall.h"
23269024Semaste#include "CGValue.h"
24269024Semaste
25269024Semastenamespace llvm {
26269024Semaste  class Constant;
27269024Semaste  class Function;
28269024Semaste  class Module;
29269024Semaste  class StructLayout;
30254721Semaste  class StructType;
31254721Semaste  class Type;
32254721Semaste  class Value;
33254721Semaste}
34254721Semaste
35254721Semastenamespace clang {
36254721Semastenamespace CodeGen {
37254721Semaste  class CodeGenFunction;
38254721Semaste}
39254721Semaste
40254721Semaste  class FieldDecl;
41254721Semaste  class ObjCAtTryStmt;
42254721Semaste  class ObjCAtThrowStmt;
43254721Semaste  class ObjCAtSynchronizedStmt;
44254721Semaste  class ObjCContainerDecl;
45254721Semaste  class ObjCCategoryImplDecl;
46254721Semaste  class ObjCImplementationDecl;
47254721Semaste  class ObjCInterfaceDecl;
48254721Semaste  class ObjCMessageExpr;
49254721Semaste  class ObjCMethodDecl;
50254721Semaste  class ObjCProtocolDecl;
51254721Semaste  class Selector;
52254721Semaste  class ObjCIvarDecl;
53254721Semaste  class ObjCStringLiteral;
54254721Semaste  class BlockDeclRefExpr;
55254721Semaste
56254721Semastenamespace CodeGen {
57254721Semaste  class CodeGenModule;
58254721Semaste  class CGBlockInfo;
59254721Semaste
60254721Semaste// FIXME: Several methods should be pure virtual but aren't to avoid the
61254721Semaste// partially-implemented subclass breaking.
62254721Semaste
63254721Semaste/// Implements runtime-specific code generation functions.
64254721Semasteclass CGObjCRuntime {
65254721Semasteprotected:
66254721Semaste  // Utility functions for unified ivar access. These need to
67254721Semaste  // eventually be folded into other places (the structure layout
68254721Semaste  // code).
69254721Semaste
70254721Semaste  /// Compute an offset to the given ivar, suitable for passing to
71254721Semaste  /// EmitValueForIvarAtOffset.  Note that the correct handling of
72254721Semaste  /// bit-fields is carefully coordinated by these two, use caution!
73254721Semaste  ///
74254721Semaste  /// The latter overload is suitable for computing the offset of a
75254721Semaste  /// sythesized ivar.
76254721Semaste  uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
77254721Semaste                                 const ObjCInterfaceDecl *OID,
78254721Semaste                                 const ObjCIvarDecl *Ivar);
79254721Semaste  uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
80254721Semaste                                 const ObjCImplementationDecl *OID,
81254721Semaste                                 const ObjCIvarDecl *Ivar);
82254721Semaste
83254721Semaste  LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
84254721Semaste                                  const ObjCInterfaceDecl *OID,
85254721Semaste                                  llvm::Value *BaseValue,
86254721Semaste                                  const ObjCIvarDecl *Ivar,
87254721Semaste                                  unsigned CVRQualifiers,
88254721Semaste                                  llvm::Value *Offset);
89254721Semaste  /// Emits a try / catch statement.  This function is intended to be called by
90254721Semaste  /// subclasses, and provides a generic mechanism for generating these, which
91254721Semaste  /// should be usable by all runtimes.  The caller must provide the functions to
92254721Semaste  /// call when entering and exiting a @catch() block, and the function used to
93254721Semaste  /// rethrow exceptions.  If the begin and end catch functions are NULL, then
94254721Semaste  /// the function assumes that the EH personality function provides the
95254721Semaste  /// thrown object directly.
96254721Semaste  void EmitTryCatchStmt(CodeGenFunction &CGF,
97254721Semaste                        const ObjCAtTryStmt &S,
98254721Semaste                        llvm::Function *beginCatchFn,
99254721Semaste                        llvm::Function *endCatchFn,
100254721Semaste                        llvm::Function *exceptionRethrowFn);
101254721Semaste  /// Emits an @synchronize() statement, using the syncEnterFn and syncExitFn
102254721Semaste  /// arguments as the functions called to lock and unlock the object.  This
103254721Semaste  /// function can be called by subclasses that use zero-cost exception
104254721Semaste  /// handling.
105254721Semaste  void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
106254721Semaste                            const ObjCAtSynchronizedStmt &S,
107254721Semaste                            llvm::Function *syncEnterFn,
108254721Semaste                            llvm::Function *syncExitFn);
109254721Semaste
110254721Semastepublic:
111254721Semaste  virtual ~CGObjCRuntime();
112254721Semaste
113254721Semaste  /// Generate the function required to register all Objective-C components in
114254721Semaste  /// this compilation unit with the runtime library.
115254721Semaste  virtual llvm::Function *ModuleInitFunction() = 0;
116254721Semaste
117254721Semaste  /// Get a selector for the specified name and type values. The
118254721Semaste  /// return value should have the LLVM type for pointer-to
119254721Semaste  /// ASTContext::getObjCSelType().
120254721Semaste  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
121254721Semaste                                   Selector Sel, bool lval=false) = 0;
122254721Semaste
123254721Semaste  /// Get a typed selector.
124254721Semaste  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
125254721Semaste                                   const ObjCMethodDecl *Method) = 0;
126254721Semaste
127254721Semaste  /// Get the type constant to catch for the given ObjC pointer type.
128254721Semaste  /// This is used externally to implement catching ObjC types in C++.
129254721Semaste  /// Runtimes which don't support this should add the appropriate
130254721Semaste  /// error to Sema.
131254721Semaste  virtual llvm::Constant *GetEHType(QualType T) = 0;
132254721Semaste
133254721Semaste  /// Generate a constant string object.
134254721Semaste  virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
135254721Semaste
136254721Semaste  /// Generate a category.  A category contains a list of methods (and
137254721Semaste  /// accompanying metadata) and a list of protocols.
138254721Semaste  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
139254721Semaste
140254721Semaste  /// Generate a class structure for this class.
141254721Semaste  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
142254721Semaste
143254721Semaste  /// Generate an Objective-C message send operation.
144254721Semaste  ///
145254721Semaste  /// \param Method - The method being called, this may be null if synthesizing
146254721Semaste  /// a property setter or getter.
147254721Semaste  virtual CodeGen::RValue
148254721Semaste  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
149254721Semaste                      ReturnValueSlot ReturnSlot,
150254721Semaste                      QualType ResultType,
151254721Semaste                      Selector Sel,
152254721Semaste                      llvm::Value *Receiver,
153254721Semaste                      const CallArgList &CallArgs,
154254721Semaste                      const ObjCInterfaceDecl *Class = 0,
155254721Semaste                      const ObjCMethodDecl *Method = 0) = 0;
156254721Semaste
157254721Semaste  /// Generate an Objective-C message send operation to the super
158254721Semaste  /// class initiated in a method for Class and with the given Self
159254721Semaste  /// object.
160254721Semaste  ///
161254721Semaste  /// \param Method - The method being called, this may be null if synthesizing
162254721Semaste  /// a property setter or getter.
163254721Semaste  virtual CodeGen::RValue
164254721Semaste  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
165254721Semaste                           ReturnValueSlot ReturnSlot,
166254721Semaste                           QualType ResultType,
167254721Semaste                           Selector Sel,
168263367Semaste                           const ObjCInterfaceDecl *Class,
169263367Semaste                           bool isCategoryImpl,
170263367Semaste                           llvm::Value *Self,
171263367Semaste                           bool IsClassMessage,
172263367Semaste                           const CallArgList &CallArgs,
173263367Semaste                           const ObjCMethodDecl *Method = 0) = 0;
174254721Semaste
175254721Semaste  /// Emit the code to return the named protocol as an object, as in a
176254721Semaste  /// @protocol expression.
177254721Semaste  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
178254721Semaste                                           const ObjCProtocolDecl *OPD) = 0;
179254721Semaste
180254721Semaste  /// Generate the named protocol.  Protocols contain method metadata but no
181254721Semaste  /// implementations.
182254721Semaste  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
183254721Semaste
184254721Semaste  /// Generate a function preamble for a method with the specified
185254721Semaste  /// types.
186254721Semaste
187254721Semaste  // FIXME: Current this just generates the Function definition, but really this
188254721Semaste  // should also be generating the loads of the parameters, as the runtime
189254721Semaste  // should have full control over how parameters are passed.
190254721Semaste  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
191254721Semaste                                         const ObjCContainerDecl *CD) = 0;
192254721Semaste
193254721Semaste  /// Return the runtime function for getting properties.
194254721Semaste  virtual llvm::Constant *GetPropertyGetFunction() = 0;
195254721Semaste
196254721Semaste  /// Return the runtime function for setting properties.
197254721Semaste  virtual llvm::Constant *GetPropertySetFunction() = 0;
198254721Semaste
199254721Semaste  // API for atomic copying of qualified aggregates in getter.
200254721Semaste  virtual llvm::Constant *GetGetStructFunction() = 0;
201254721Semaste  // API for atomic copying of qualified aggregates in setter.
202254721Semaste  virtual llvm::Constant *GetSetStructFunction() = 0;
203254721Semaste
204254721Semaste  /// GetClass - Return a reference to the class for the given
205254721Semaste  /// interface decl.
206254721Semaste  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
207254721Semaste                                const ObjCInterfaceDecl *OID) = 0;
208254721Semaste
209254721Semaste  /// EnumerationMutationFunction - Return the function that's called by the
210254721Semaste  /// compiler when a mutation is detected during foreach iteration.
211254721Semaste  virtual llvm::Constant *EnumerationMutationFunction() = 0;
212254721Semaste
213254721Semaste  virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
214254721Semaste                                    const ObjCAtSynchronizedStmt &S) = 0;
215254721Semaste  virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
216254721Semaste                           const ObjCAtTryStmt &S) = 0;
217254721Semaste  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
218254721Semaste                             const ObjCAtThrowStmt &S) = 0;
219254721Semaste  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
220254721Semaste                                        llvm::Value *AddrWeakObj) = 0;
221254721Semaste  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
222254721Semaste                                  llvm::Value *src, llvm::Value *dest) = 0;
223254721Semaste  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
224254721Semaste                                    llvm::Value *src, llvm::Value *dest,
225254721Semaste                                    bool threadlocal=false) = 0;
226254721Semaste  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
227254721Semaste                                  llvm::Value *src, llvm::Value *dest,
228254721Semaste                                  llvm::Value *ivarOffset) = 0;
229254721Semaste  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
230254721Semaste                                        llvm::Value *src, llvm::Value *dest) = 0;
231254721Semaste
232254721Semaste  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
233254721Semaste                                      QualType ObjectTy,
234254721Semaste                                      llvm::Value *BaseValue,
235254721Semaste                                      const ObjCIvarDecl *Ivar,
236254721Semaste                                      unsigned CVRQualifiers) = 0;
237254721Semaste  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
238254721Semaste                                      const ObjCInterfaceDecl *Interface,
239254721Semaste                                      const ObjCIvarDecl *Ivar) = 0;
240254721Semaste  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
241254721Semaste                                        llvm::Value *DestPtr,
242254721Semaste                                        llvm::Value *SrcPtr,
243254721Semaste                                        llvm::Value *Size) = 0;
244254721Semaste  virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
245254721Semaste                                  const CodeGen::CGBlockInfo &blockInfo) = 0;
246254721Semaste};
247254721Semaste
248254721Semaste/// Creates an instance of an Objective-C runtime class.
249254721Semaste//TODO: This should include some way of selecting which runtime to target.
250254721SemasteCGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
251254721SemasteCGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
252254721Semaste}
253254721Semaste}
254254721Semaste#endif
255254721Semaste