CGObjCRuntime.h revision 223017
1193326Sed//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This provides an abstract class for Objective-C code generation.  Concrete
11193326Sed// subclasses of this implement code generation for specific Objective-C
12193326Sed// runtime libraries.
13193326Sed//
14193326Sed//===----------------------------------------------------------------------===//
15193326Sed
16193326Sed#ifndef CLANG_CODEGEN_OBCJRUNTIME_H
17193326Sed#define CLANG_CODEGEN_OBCJRUNTIME_H
18193326Sed#include "clang/Basic/IdentifierTable.h" // Selector
19193326Sed#include "clang/AST/DeclObjC.h"
20193326Sed
21193326Sed#include "CGBuilder.h"
22193326Sed#include "CGCall.h"
23193326Sed#include "CGValue.h"
24193326Sed
25193326Sednamespace llvm {
26193326Sed  class Constant;
27193326Sed  class Function;
28193326Sed  class Module;
29193326Sed  class StructLayout;
30193326Sed  class StructType;
31193326Sed  class Type;
32193326Sed  class Value;
33193326Sed}
34193326Sed
35193326Sednamespace clang {
36193326Sednamespace CodeGen {
37193326Sed  class CodeGenFunction;
38193326Sed}
39193326Sed
40193326Sed  class FieldDecl;
41193326Sed  class ObjCAtTryStmt;
42193326Sed  class ObjCAtThrowStmt;
43193326Sed  class ObjCAtSynchronizedStmt;
44193326Sed  class ObjCContainerDecl;
45193326Sed  class ObjCCategoryImplDecl;
46193326Sed  class ObjCImplementationDecl;
47193326Sed  class ObjCInterfaceDecl;
48193326Sed  class ObjCMessageExpr;
49193326Sed  class ObjCMethodDecl;
50193326Sed  class ObjCProtocolDecl;
51193326Sed  class Selector;
52193326Sed  class ObjCIvarDecl;
53193326Sed  class ObjCStringLiteral;
54212904Sdim  class BlockDeclRefExpr;
55193326Sed
56193326Sednamespace CodeGen {
57193326Sed  class CodeGenModule;
58218893Sdim  class CGBlockInfo;
59193326Sed
60193326Sed// FIXME: Several methods should be pure virtual but aren't to avoid the
61193326Sed// partially-implemented subclass breaking.
62193326Sed
63193326Sed/// Implements runtime-specific code generation functions.
64193326Sedclass CGObjCRuntime {
65206275Srdivackyprotected:
66193326Sed  // Utility functions for unified ivar access. These need to
67193326Sed  // eventually be folded into other places (the structure layout
68193326Sed  // code).
69193326Sed
70193326Sed  /// Compute an offset to the given ivar, suitable for passing to
71193326Sed  /// EmitValueForIvarAtOffset.  Note that the correct handling of
72193326Sed  /// bit-fields is carefully coordinated by these two, use caution!
73193326Sed  ///
74193326Sed  /// The latter overload is suitable for computing the offset of a
75193326Sed  /// sythesized ivar.
76193326Sed  uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
77193326Sed                                 const ObjCInterfaceDecl *OID,
78193326Sed                                 const ObjCIvarDecl *Ivar);
79193326Sed  uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
80193326Sed                                 const ObjCImplementationDecl *OID,
81193326Sed                                 const ObjCIvarDecl *Ivar);
82193326Sed
83193326Sed  LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
84193326Sed                                  const ObjCInterfaceDecl *OID,
85193326Sed                                  llvm::Value *BaseValue,
86193326Sed                                  const ObjCIvarDecl *Ivar,
87193326Sed                                  unsigned CVRQualifiers,
88198092Srdivacky                                  llvm::Value *Offset);
89221345Sdim  /// Emits a try / catch statement.  This function is intended to be called by
90221345Sdim  /// subclasses, and provides a generic mechanism for generating these, which
91221345Sdim  /// should be usable by all runtimes.  The caller must provide the functions to
92221345Sdim  /// call when entering and exiting a @catch() block, and the function used to
93221345Sdim  /// rethrow exceptions.  If the begin and end catch functions are NULL, then
94221345Sdim  /// the function assumes that the EH personality function provides the
95221345Sdim  /// thrown object directly.
96221345Sdim  void EmitTryCatchStmt(CodeGenFunction &CGF,
97221345Sdim                        const ObjCAtTryStmt &S,
98223017Sdim                        llvm::Constant *beginCatchFn,
99223017Sdim                        llvm::Constant *endCatchFn,
100223017Sdim                        llvm::Constant *exceptionRethrowFn);
101221345Sdim  /// Emits an @synchronize() statement, using the syncEnterFn and syncExitFn
102221345Sdim  /// arguments as the functions called to lock and unlock the object.  This
103221345Sdim  /// function can be called by subclasses that use zero-cost exception
104221345Sdim  /// handling.
105221345Sdim  void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
106221345Sdim                            const ObjCAtSynchronizedStmt &S,
107221345Sdim                            llvm::Function *syncEnterFn,
108221345Sdim                            llvm::Function *syncExitFn);
109193326Sed
110193326Sedpublic:
111193326Sed  virtual ~CGObjCRuntime();
112193326Sed
113193326Sed  /// Generate the function required to register all Objective-C components in
114193326Sed  /// this compilation unit with the runtime library.
115193326Sed  virtual llvm::Function *ModuleInitFunction() = 0;
116193326Sed
117193326Sed  /// Get a selector for the specified name and type values. The
118193326Sed  /// return value should have the LLVM type for pointer-to
119193326Sed  /// ASTContext::getObjCSelType().
120193326Sed  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
121210299Sed                                   Selector Sel, bool lval=false) = 0;
122193326Sed
123198092Srdivacky  /// Get a typed selector.
124193326Sed  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
125193326Sed                                   const ObjCMethodDecl *Method) = 0;
126193326Sed
127212904Sdim  /// Get the type constant to catch for the given ObjC pointer type.
128212904Sdim  /// This is used externally to implement catching ObjC types in C++.
129212904Sdim  /// Runtimes which don't support this should add the appropriate
130212904Sdim  /// error to Sema.
131212904Sdim  virtual llvm::Constant *GetEHType(QualType T) = 0;
132212904Sdim
133193326Sed  /// Generate a constant string object.
134202879Srdivacky  virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
135193326Sed
136193326Sed  /// Generate a category.  A category contains a list of methods (and
137193326Sed  /// accompanying metadata) and a list of protocols.
138193326Sed  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
139193326Sed
140221345Sdim  /// Generate a class structure for this class.
141193326Sed  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
142198092Srdivacky
143198092Srdivacky  /// Generate an Objective-C message send operation.
144198092Srdivacky  ///
145198092Srdivacky  /// \param Method - The method being called, this may be null if synthesizing
146198092Srdivacky  /// a property setter or getter.
147198092Srdivacky  virtual CodeGen::RValue
148193326Sed  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
149208600Srdivacky                      ReturnValueSlot ReturnSlot,
150193326Sed                      QualType ResultType,
151193326Sed                      Selector Sel,
152193326Sed                      llvm::Value *Receiver,
153193326Sed                      const CallArgList &CallArgs,
154207619Srdivacky                      const ObjCInterfaceDecl *Class = 0,
155198092Srdivacky                      const ObjCMethodDecl *Method = 0) = 0;
156193326Sed
157193326Sed  /// Generate an Objective-C message send operation to the super
158193326Sed  /// class initiated in a method for Class and with the given Self
159193326Sed  /// object.
160198092Srdivacky  ///
161198092Srdivacky  /// \param Method - The method being called, this may be null if synthesizing
162198092Srdivacky  /// a property setter or getter.
163193326Sed  virtual CodeGen::RValue
164193326Sed  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
165208600Srdivacky                           ReturnValueSlot ReturnSlot,
166193326Sed                           QualType ResultType,
167193326Sed                           Selector Sel,
168193326Sed                           const ObjCInterfaceDecl *Class,
169193326Sed                           bool isCategoryImpl,
170193326Sed                           llvm::Value *Self,
171193326Sed                           bool IsClassMessage,
172198092Srdivacky                           const CallArgList &CallArgs,
173198092Srdivacky                           const ObjCMethodDecl *Method = 0) = 0;
174193326Sed
175193326Sed  /// Emit the code to return the named protocol as an object, as in a
176193326Sed  /// @protocol expression.
177193326Sed  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
178193326Sed                                           const ObjCProtocolDecl *OPD) = 0;
179193326Sed
180198092Srdivacky  /// Generate the named protocol.  Protocols contain method metadata but no
181198092Srdivacky  /// implementations.
182193326Sed  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
183193326Sed
184193326Sed  /// Generate a function preamble for a method with the specified
185198092Srdivacky  /// types.
186193326Sed
187193326Sed  // FIXME: Current this just generates the Function definition, but really this
188193326Sed  // should also be generating the loads of the parameters, as the runtime
189193326Sed  // should have full control over how parameters are passed.
190198092Srdivacky  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
191193326Sed                                         const ObjCContainerDecl *CD) = 0;
192193326Sed
193193326Sed  /// Return the runtime function for getting properties.
194193326Sed  virtual llvm::Constant *GetPropertyGetFunction() = 0;
195198092Srdivacky
196193326Sed  /// Return the runtime function for setting properties.
197193326Sed  virtual llvm::Constant *GetPropertySetFunction() = 0;
198193326Sed
199218893Sdim  // API for atomic copying of qualified aggregates in getter.
200218893Sdim  virtual llvm::Constant *GetGetStructFunction() = 0;
201218893Sdim  // API for atomic copying of qualified aggregates in setter.
202218893Sdim  virtual llvm::Constant *GetSetStructFunction() = 0;
203207619Srdivacky
204193326Sed  /// GetClass - Return a reference to the class for the given
205193326Sed  /// interface decl.
206198092Srdivacky  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
207193326Sed                                const ObjCInterfaceDecl *OID) = 0;
208193326Sed
209193326Sed  /// EnumerationMutationFunction - Return the function that's called by the
210193326Sed  /// compiler when a mutation is detected during foreach iteration.
211193326Sed  virtual llvm::Constant *EnumerationMutationFunction() = 0;
212198092Srdivacky
213210299Sed  virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
214210299Sed                                    const ObjCAtSynchronizedStmt &S) = 0;
215210299Sed  virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
216210299Sed                           const ObjCAtTryStmt &S) = 0;
217193326Sed  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
218193326Sed                             const ObjCAtThrowStmt &S) = 0;
219193326Sed  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
220193326Sed                                        llvm::Value *AddrWeakObj) = 0;
221193326Sed  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
222193326Sed                                  llvm::Value *src, llvm::Value *dest) = 0;
223193326Sed  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
224212904Sdim                                    llvm::Value *src, llvm::Value *dest,
225212904Sdim                                    bool threadlocal=false) = 0;
226193326Sed  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
227198092Srdivacky                                  llvm::Value *src, llvm::Value *dest,
228198092Srdivacky                                  llvm::Value *ivarOffset) = 0;
229193326Sed  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
230193326Sed                                        llvm::Value *src, llvm::Value *dest) = 0;
231198092Srdivacky
232193326Sed  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
233193326Sed                                      QualType ObjectTy,
234193326Sed                                      llvm::Value *BaseValue,
235193326Sed                                      const ObjCIvarDecl *Ivar,
236193326Sed                                      unsigned CVRQualifiers) = 0;
237193326Sed  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
238193326Sed                                      const ObjCInterfaceDecl *Interface,
239193326Sed                                      const ObjCIvarDecl *Ivar) = 0;
240198092Srdivacky  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
241198092Srdivacky                                        llvm::Value *DestPtr,
242198092Srdivacky                                        llvm::Value *SrcPtr,
243210299Sed                                        llvm::Value *Size) = 0;
244218893Sdim  virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
245218893Sdim                                  const CodeGen::CGBlockInfo &blockInfo) = 0;
246223017Sdim  virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) = 0;
247193326Sed};
248193326Sed
249198092Srdivacky/// Creates an instance of an Objective-C runtime class.
250193326Sed//TODO: This should include some way of selecting which runtime to target.
251193326SedCGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
252193326SedCGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
253193326Sed}
254193326Sed}
255193326Sed#endif
256