CGObjCRuntime.h revision 212904
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#include <string>
21193326Sed
22193326Sed#include "CGBuilder.h"
23193326Sed#include "CGCall.h"
24193326Sed#include "CGValue.h"
25193326Sed
26193326Sednamespace llvm {
27193326Sed  class Constant;
28193326Sed  class Function;
29193326Sed  class Module;
30193326Sed  class StructLayout;
31193326Sed  class StructType;
32193326Sed  class Type;
33193326Sed  class Value;
34193326Sed}
35193326Sed
36193326Sednamespace clang {
37193326Sednamespace CodeGen {
38193326Sed  class CodeGenFunction;
39193326Sed}
40193326Sed
41193326Sed  class FieldDecl;
42193326Sed  class ObjCAtTryStmt;
43193326Sed  class ObjCAtThrowStmt;
44193326Sed  class ObjCAtSynchronizedStmt;
45193326Sed  class ObjCContainerDecl;
46193326Sed  class ObjCCategoryImplDecl;
47193326Sed  class ObjCImplementationDecl;
48193326Sed  class ObjCInterfaceDecl;
49193326Sed  class ObjCMessageExpr;
50193326Sed  class ObjCMethodDecl;
51193326Sed  class ObjCProtocolDecl;
52193326Sed  class Selector;
53193326Sed  class ObjCIvarDecl;
54193326Sed  class ObjCStringLiteral;
55212904Sdim  class BlockDeclRefExpr;
56193326Sed
57193326Sednamespace CodeGen {
58193326Sed  class CodeGenModule;
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);
89193326Sed
90193326Sedpublic:
91193326Sed  virtual ~CGObjCRuntime();
92193326Sed
93193326Sed  /// Generate the function required to register all Objective-C components in
94193326Sed  /// this compilation unit with the runtime library.
95193326Sed  virtual llvm::Function *ModuleInitFunction() = 0;
96193326Sed
97193326Sed  /// Get a selector for the specified name and type values. The
98193326Sed  /// return value should have the LLVM type for pointer-to
99193326Sed  /// ASTContext::getObjCSelType().
100193326Sed  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
101210299Sed                                   Selector Sel, bool lval=false) = 0;
102193326Sed
103198092Srdivacky  /// Get a typed selector.
104193326Sed  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
105193326Sed                                   const ObjCMethodDecl *Method) = 0;
106193326Sed
107212904Sdim  /// Get the type constant to catch for the given ObjC pointer type.
108212904Sdim  /// This is used externally to implement catching ObjC types in C++.
109212904Sdim  /// Runtimes which don't support this should add the appropriate
110212904Sdim  /// error to Sema.
111212904Sdim  virtual llvm::Constant *GetEHType(QualType T) = 0;
112212904Sdim
113193326Sed  /// Generate a constant string object.
114202879Srdivacky  virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
115193326Sed
116193326Sed  /// Generate a category.  A category contains a list of methods (and
117193326Sed  /// accompanying metadata) and a list of protocols.
118193326Sed  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
119193326Sed
120193326Sed  /// Generate a class stucture for this class.
121193326Sed  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
122198092Srdivacky
123198092Srdivacky  /// Generate an Objective-C message send operation.
124198092Srdivacky  ///
125198092Srdivacky  /// \param Method - The method being called, this may be null if synthesizing
126198092Srdivacky  /// a property setter or getter.
127198092Srdivacky  virtual CodeGen::RValue
128193326Sed  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
129208600Srdivacky                      ReturnValueSlot ReturnSlot,
130193326Sed                      QualType ResultType,
131193326Sed                      Selector Sel,
132193326Sed                      llvm::Value *Receiver,
133193326Sed                      const CallArgList &CallArgs,
134207619Srdivacky                      const ObjCInterfaceDecl *Class = 0,
135198092Srdivacky                      const ObjCMethodDecl *Method = 0) = 0;
136193326Sed
137193326Sed  /// Generate an Objective-C message send operation to the super
138193326Sed  /// class initiated in a method for Class and with the given Self
139193326Sed  /// object.
140198092Srdivacky  ///
141198092Srdivacky  /// \param Method - The method being called, this may be null if synthesizing
142198092Srdivacky  /// a property setter or getter.
143193326Sed  virtual CodeGen::RValue
144193326Sed  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
145208600Srdivacky                           ReturnValueSlot ReturnSlot,
146193326Sed                           QualType ResultType,
147193326Sed                           Selector Sel,
148193326Sed                           const ObjCInterfaceDecl *Class,
149193326Sed                           bool isCategoryImpl,
150193326Sed                           llvm::Value *Self,
151193326Sed                           bool IsClassMessage,
152198092Srdivacky                           const CallArgList &CallArgs,
153198092Srdivacky                           const ObjCMethodDecl *Method = 0) = 0;
154193326Sed
155193326Sed  /// Emit the code to return the named protocol as an object, as in a
156193326Sed  /// @protocol expression.
157193326Sed  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
158193326Sed                                           const ObjCProtocolDecl *OPD) = 0;
159193326Sed
160198092Srdivacky  /// Generate the named protocol.  Protocols contain method metadata but no
161198092Srdivacky  /// implementations.
162193326Sed  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
163193326Sed
164193326Sed  /// Generate a function preamble for a method with the specified
165198092Srdivacky  /// types.
166193326Sed
167193326Sed  // FIXME: Current this just generates the Function definition, but really this
168193326Sed  // should also be generating the loads of the parameters, as the runtime
169193326Sed  // should have full control over how parameters are passed.
170198092Srdivacky  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
171193326Sed                                         const ObjCContainerDecl *CD) = 0;
172193326Sed
173193326Sed  /// Return the runtime function for getting properties.
174193326Sed  virtual llvm::Constant *GetPropertyGetFunction() = 0;
175198092Srdivacky
176193326Sed  /// Return the runtime function for setting properties.
177193326Sed  virtual llvm::Constant *GetPropertySetFunction() = 0;
178193326Sed
179207619Srdivacky  // API for atomic copying of qualified aggregates in setter/getter.
180207619Srdivacky  virtual llvm::Constant *GetCopyStructFunction() = 0;
181207619Srdivacky
182193326Sed  /// GetClass - Return a reference to the class for the given
183193326Sed  /// interface decl.
184198092Srdivacky  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
185193326Sed                                const ObjCInterfaceDecl *OID) = 0;
186193326Sed
187193326Sed  /// EnumerationMutationFunction - Return the function that's called by the
188193326Sed  /// compiler when a mutation is detected during foreach iteration.
189193326Sed  virtual llvm::Constant *EnumerationMutationFunction() = 0;
190198092Srdivacky
191210299Sed  virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
192210299Sed                                    const ObjCAtSynchronizedStmt &S) = 0;
193210299Sed  virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
194210299Sed                           const ObjCAtTryStmt &S) = 0;
195193326Sed  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
196193326Sed                             const ObjCAtThrowStmt &S) = 0;
197193326Sed  virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
198193326Sed                                        llvm::Value *AddrWeakObj) = 0;
199193326Sed  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
200193326Sed                                  llvm::Value *src, llvm::Value *dest) = 0;
201193326Sed  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
202212904Sdim                                    llvm::Value *src, llvm::Value *dest,
203212904Sdim                                    bool threadlocal=false) = 0;
204193326Sed  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
205198092Srdivacky                                  llvm::Value *src, llvm::Value *dest,
206198092Srdivacky                                  llvm::Value *ivarOffset) = 0;
207193326Sed  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
208193326Sed                                        llvm::Value *src, llvm::Value *dest) = 0;
209198092Srdivacky
210193326Sed  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
211193326Sed                                      QualType ObjectTy,
212193326Sed                                      llvm::Value *BaseValue,
213193326Sed                                      const ObjCIvarDecl *Ivar,
214193326Sed                                      unsigned CVRQualifiers) = 0;
215193326Sed  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
216193326Sed                                      const ObjCInterfaceDecl *Interface,
217193326Sed                                      const ObjCIvarDecl *Ivar) = 0;
218198092Srdivacky  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
219198092Srdivacky                                        llvm::Value *DestPtr,
220198092Srdivacky                                        llvm::Value *SrcPtr,
221210299Sed                                        llvm::Value *Size) = 0;
222212904Sdim  virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
223212904Sdim                  const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &) = 0;
224212904Sdim
225193326Sed};
226193326Sed
227198092Srdivacky/// Creates an instance of an Objective-C runtime class.
228193326Sed//TODO: This should include some way of selecting which runtime to target.
229193326SedCGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
230193326SedCGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
231193326SedCGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
232193326Sed}
233193326Sed}
234193326Sed#endif
235