]> granicus.if.org Git - clang/blob - lib/CodeGen/CGObjCRuntime.h
Add a ComputeIvarBaseOffset overload taking an implementation
[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 Function;
29   class Module;
30   class StructLayout;
31   class StructType;
32   class Type;
33   class Value;
34 }
35
36 namespace clang {
37 namespace CodeGen {
38   class CodeGenFunction;
39 }
40
41   class FieldDecl;
42   class ObjCAtTryStmt;
43   class ObjCAtThrowStmt;
44   class ObjCAtSynchronizedStmt;
45   class ObjCContainerDecl;
46   class ObjCCategoryImplDecl;
47   class ObjCImplementationDecl;
48   class ObjCInterfaceDecl;
49   class ObjCMessageExpr;
50   class ObjCMethodDecl;
51   class ObjCProtocolDecl;
52   class Selector;
53   class ObjCIvarDecl;
54   class ObjCStringLiteral;
55
56 namespace CodeGen {
57   class CodeGenModule;
58
59 //FIXME Several methods should be pure virtual but aren't to avoid the
60 //partially-implemented subclass breaking.
61
62 /// Implements runtime-specific code generation functions.
63 class CGObjCRuntime {
64 public:
65   // Utility functions for unified ivar access. These need to
66   // eventually be folded into other places (the structure layout
67   // code).
68
69 protected:
70   /// Compute an offset to the given ivar, suitable for passing to
71   /// EmitValueForIvarAtOffset.  Note that the correct handling of
72   /// bit-fields is carefully coordinated by these two, use caution!
73   ///
74   /// The latter overload is suitable for computing the offset of a
75   /// sythesized ivar.
76   uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
77                                  const ObjCInterfaceDecl *OID,
78                                  const ObjCIvarDecl *Ivar);
79   uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
80                                  const ObjCImplementationDecl *OID,
81                                  const ObjCIvarDecl *Ivar);
82
83   LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
84                                   const ObjCInterfaceDecl *OID,
85                                   llvm::Value *BaseValue,
86                                   const ObjCIvarDecl *Ivar,
87                                   unsigned CVRQualifiers,
88                                   llvm::Value *Offset);  
89
90 public:
91   virtual ~CGObjCRuntime();
92
93   /// Generate the function required to register all Objective-C components in
94   /// this compilation unit with the runtime library.
95   virtual llvm::Function *ModuleInitFunction() = 0;
96
97   /// Get a selector for the specified name and type values. The
98   /// return value should have the LLVM type for pointer-to
99   /// ASTContext::getObjCSelType().
100   virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
101                                    Selector Sel) = 0;
102
103   /// Generate a constant string object.
104   virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0;
105
106   /// Generate a category.  A category contains a list of methods (and
107   /// accompanying metadata) and a list of protocols.
108   virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
109
110   /// Generate a class stucture for this class.
111   virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
112   
113   /// Generate an Objective-C message send operation.
114   virtual CodeGen::RValue 
115   GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
116                       QualType ResultType,
117                       Selector Sel,
118                       llvm::Value *Receiver,
119                       bool IsClassMessage,
120                       const CallArgList &CallArgs) = 0;
121
122   /// Generate an Objective-C message send operation to the super
123   /// class initiated in a method for Class and with the given Self
124   /// object.
125   virtual CodeGen::RValue
126   GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
127                            QualType ResultType,
128                            Selector Sel,
129                            const ObjCInterfaceDecl *Class,
130                            bool isCategoryImpl,
131                            llvm::Value *Self,
132                            bool IsClassMessage,
133                            const CallArgList &CallArgs) = 0;
134
135   /// Emit the code to return the named protocol as an object, as in a
136   /// @protocol expression.
137   virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
138                                            const ObjCProtocolDecl *OPD) = 0;
139
140   /// Generate the named protocol.  Protocols contain method metadata but no 
141   /// implementations. 
142   virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
143
144   /// Generate a function preamble for a method with the specified
145   /// types.  
146
147   // FIXME: Current this just generates the Function definition, but
148   // really this should also be generating the loads of the
149   // parameters, as the runtime should have full control over how
150   // parameters are passed.
151   virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 
152                                          const ObjCContainerDecl *CD) = 0;
153
154   /// Return the runtime function for getting properties.
155   virtual llvm::Constant *GetPropertyGetFunction() = 0;
156   
157   /// Return the runtime function for setting properties.
158   virtual llvm::Constant *GetPropertySetFunction() = 0;
159
160   /// GetClass - Return a reference to the class for the given
161   /// interface decl.
162   virtual llvm::Value *GetClass(CGBuilderTy &Builder, 
163                                 const ObjCInterfaceDecl *OID) = 0;
164
165   /// EnumerationMutationFunction - Return the function that's called by the
166   /// compiler when a mutation is detected during foreach iteration.
167   virtual llvm::Constant *EnumerationMutationFunction() = 0;
168   
169   virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
170                                          const Stmt &S) = 0;
171   virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
172                              const ObjCAtThrowStmt &S) = 0;
173   virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
174                                         llvm::Value *AddrWeakObj) = 0;
175   virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
176                                   llvm::Value *src, llvm::Value *dest) = 0;
177   virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
178                                     llvm::Value *src, llvm::Value *dest) = 0;
179   virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
180                                   llvm::Value *src, llvm::Value *dest) = 0;
181   virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
182                                         llvm::Value *src, llvm::Value *dest) = 0;
183   
184   virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
185                                       QualType ObjectTy,
186                                       llvm::Value *BaseValue,
187                                       const ObjCIvarDecl *Ivar,
188                                       unsigned CVRQualifiers) = 0;
189   virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
190                                       const ObjCInterfaceDecl *Interface,
191                                       const ObjCIvarDecl *Ivar) = 0;
192 };
193
194 /// Creates an instance of an Objective-C runtime class.  
195 //TODO: This should include some way of selecting which runtime to target.
196 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
197 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
198 CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
199 }
200 }
201 #endif