CGObjCRuntime.h revision 74391b48b4791cded373683a3baf67314f358d50
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 "llvm/ADT/SmallVector.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 Type;
29  class Value;
30  class Module;
31  class Function;
32}
33
34namespace clang {
35namespace CodeGen {
36  class CodeGenFunction;
37}
38
39  class ObjCAtTryStmt;
40  class ObjCAtThrowStmt;
41  class ObjCAtSynchronizedStmt;
42  class ObjCContainerDecl;
43  class ObjCCategoryImplDecl;
44  class ObjCImplementationDecl;
45  class ObjCInterfaceDecl;
46  class ObjCMessageExpr;
47  class ObjCMethodDecl;
48  class ObjCProtocolDecl;
49  class Selector;
50  class ObjCIvarDecl;
51
52namespace CodeGen {
53  class CodeGenModule;
54
55//FIXME Several methods should be pure virtual but aren't to avoid the
56//partially-implemented subclass breaking.
57
58/// Implements runtime-specific code generation functions.
59class CGObjCRuntime {
60
61public:
62  virtual ~CGObjCRuntime();
63
64  /// Generate the function required to register all Objective-C components in
65  /// this compilation unit with the runtime library.
66  virtual llvm::Function *ModuleInitFunction() = 0;
67
68  /// Get a selector for the specified name and type values. The
69  /// return value should have the LLVM type for pointer-to
70  /// ASTContext::getObjCSelType().
71  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
72                                   Selector Sel) = 0;
73
74  /// Generate a constant string object.
75  virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
76
77  /// Generate a category.  A category contains a list of methods (and
78  /// accompanying metadata) and a list of protocols.
79  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
80
81  /// Generate a class stucture for this class.
82  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
83
84  /// Generate an Objective-C message send operation.
85  virtual CodeGen::RValue
86  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
87                      QualType ResultType,
88                      Selector Sel,
89                      llvm::Value *Receiver,
90                      bool IsClassMessage,
91                      const CallArgList &CallArgs) = 0;
92
93  /// Generate an Objective-C message send operation to the super
94  /// class initiated in a method for Class and with the given Self
95  /// object.
96  virtual CodeGen::RValue
97  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
98                           QualType ResultType,
99                           Selector Sel,
100                           const ObjCInterfaceDecl *Class,
101                           bool isCategoryImpl,
102                           llvm::Value *Self,
103                           bool IsClassMessage,
104                           const CallArgList &CallArgs) = 0;
105
106  /// Emit the code to return the named protocol as an object, as in a
107  /// @protocol expression.
108  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
109                                           const ObjCProtocolDecl *OPD) = 0;
110
111  /// Generate the named protocol.  Protocols contain method metadata but no
112  /// implementations.
113  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
114
115  /// Generate a function preamble for a method with the specified
116  /// types.
117
118  // FIXME: Current this just generates the Function definition, but
119  // really this should also be generating the loads of the
120  // parameters, as the runtime should have full control over how
121  // parameters are passed.
122  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
123                                         const ObjCContainerDecl *CD) = 0;
124
125  /// Return the runtime function for getting properties.
126  virtual llvm::Constant *GetPropertyGetFunction() = 0;
127
128  /// Return the runtime function for setting properties.
129  virtual llvm::Constant *GetPropertySetFunction() = 0;
130
131  /// GetClass - Return a reference to the class for the given
132  /// interface decl.
133  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
134                                const ObjCInterfaceDecl *OID) = 0;
135
136  /// EnumerationMutationFunction - Return the function that's called by the
137  /// compiler when a mutation is detected during foreach iteration.
138  virtual llvm::Constant *EnumerationMutationFunction() = 0;
139
140  /// If instance variable addresses are determined at runtime then this should
141  /// return true, otherwise instance variables will be accessed directly from
142  /// the structure.  If this returns true then @defs is invalid for this
143  /// runtime and a warning should be generated.
144  virtual bool LateBoundIVars() const { return false; }
145
146  virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
147                                         const Stmt &S) = 0;
148  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
149                             const ObjCAtThrowStmt &S) = 0;
150  virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
151                                         llvm::Value *AddrWeakObj) = 0;
152  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
153                                  llvm::Value *src, llvm::Value *dest) = 0;
154  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
155                                    llvm::Value *src, llvm::Value *dest) = 0;
156  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
157                                  llvm::Value *src, llvm::Value *dest) = 0;
158  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
159                                        llvm::Value *src, llvm::Value *dest) = 0;
160
161  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
162                                      QualType ObjectTy,
163                                      llvm::Value *BaseValue,
164                                      const ObjCIvarDecl *Ivar,
165                                      const FieldDecl *Field,
166                                      unsigned CVRQualifiers) = 0;
167  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
168                                      ObjCInterfaceDecl *Interface,
169                                      const ObjCIvarDecl *Ivar) = 0;
170};
171
172/// Creates an instance of an Objective-C runtime class.
173//TODO: This should include some way of selecting which runtime to target.
174CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
175CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
176CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
177}
178}
179#endif
180