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