1 //===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides an abstract class for Objective-C code generation. Concrete
11 // subclasses of this implement code generation for specific Objective-C
14 //===----------------------------------------------------------------------===//
16 #ifndef CLANG_CODEGEN_OBCJRUNTIME_H
17 #define CLANG_CODEGEN_OBCJRUNTIME_H
18 #include "clang/Basic/IdentifierTable.h" // Selector
19 #include "clang/AST/DeclObjC.h"
22 #include "CGBuilder.h"
38 class CodeGenFunction;
43 class ObjCAtThrowStmt;
44 class ObjCAtSynchronizedStmt;
45 class ObjCContainerDecl;
46 class ObjCCategoryImplDecl;
47 class ObjCImplementationDecl;
48 class ObjCInterfaceDecl;
49 class ObjCMessageExpr;
51 class ObjCProtocolDecl;
54 class ObjCStringLiteral;
55 class BlockDeclRefExpr;
60 // FIXME: Several methods should be pure virtual but aren't to avoid the
61 // partially-implemented subclass breaking.
63 /// Implements runtime-specific code generation functions.
66 // Utility functions for unified ivar access. These need to
67 // eventually be folded into other places (the structure layout
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!
74 /// The latter overload is suitable for computing the offset of a
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);
83 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
84 const ObjCInterfaceDecl *OID,
85 llvm::Value *BaseValue,
86 const ObjCIvarDecl *Ivar,
87 unsigned CVRQualifiers,
91 virtual ~CGObjCRuntime();
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;
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, bool lval=false) = 0;
103 /// Get a typed selector.
104 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
105 const ObjCMethodDecl *Method) = 0;
107 /// Get the type constant to catch for the given ObjC pointer type.
108 /// This is used externally to implement catching ObjC types in C++.
109 /// Runtimes which don't support this should add the appropriate
111 virtual llvm::Constant *GetEHType(QualType T) = 0;
113 /// Generate a constant string object.
114 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
116 /// Generate a category. A category contains a list of methods (and
117 /// accompanying metadata) and a list of protocols.
118 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
120 /// Generate a class stucture for this class.
121 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
123 /// Generate an Objective-C message send operation.
125 /// \param Method - The method being called, this may be null if synthesizing
126 /// a property setter or getter.
127 virtual CodeGen::RValue
128 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
129 ReturnValueSlot ReturnSlot,
132 llvm::Value *Receiver,
133 const CallArgList &CallArgs,
134 const ObjCInterfaceDecl *Class = 0,
135 const ObjCMethodDecl *Method = 0) = 0;
137 /// Generate an Objective-C message send operation to the super
138 /// class initiated in a method for Class and with the given Self
141 /// \param Method - The method being called, this may be null if synthesizing
142 /// a property setter or getter.
143 virtual CodeGen::RValue
144 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
145 ReturnValueSlot ReturnSlot,
148 const ObjCInterfaceDecl *Class,
152 const CallArgList &CallArgs,
153 const ObjCMethodDecl *Method = 0) = 0;
155 /// Emit the code to return the named protocol as an object, as in a
156 /// @protocol expression.
157 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
158 const ObjCProtocolDecl *OPD) = 0;
160 /// Generate the named protocol. Protocols contain method metadata but no
162 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
164 /// Generate a function preamble for a method with the specified
167 // FIXME: Current this just generates the Function definition, but really this
168 // should also be generating the loads of the parameters, as the runtime
169 // should have full control over how parameters are passed.
170 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
171 const ObjCContainerDecl *CD) = 0;
173 /// Return the runtime function for getting properties.
174 virtual llvm::Constant *GetPropertyGetFunction() = 0;
176 /// Return the runtime function for setting properties.
177 virtual llvm::Constant *GetPropertySetFunction() = 0;
179 // API for atomic copying of qualified aggregates in setter/getter.
180 virtual llvm::Constant *GetCopyStructFunction() = 0;
182 /// GetClass - Return a reference to the class for the given
184 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
185 const ObjCInterfaceDecl *OID) = 0;
187 /// EnumerationMutationFunction - Return the function that's called by the
188 /// compiler when a mutation is detected during foreach iteration.
189 virtual llvm::Constant *EnumerationMutationFunction() = 0;
191 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
192 const ObjCAtSynchronizedStmt &S) = 0;
193 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
194 const ObjCAtTryStmt &S) = 0;
195 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
196 const ObjCAtThrowStmt &S) = 0;
197 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
198 llvm::Value *AddrWeakObj) = 0;
199 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
200 llvm::Value *src, llvm::Value *dest) = 0;
201 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
202 llvm::Value *src, llvm::Value *dest,
203 bool threadlocal=false) = 0;
204 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
205 llvm::Value *src, llvm::Value *dest,
206 llvm::Value *ivarOffset) = 0;
207 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
208 llvm::Value *src, llvm::Value *dest) = 0;
210 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
212 llvm::Value *BaseValue,
213 const ObjCIvarDecl *Ivar,
214 unsigned CVRQualifiers) = 0;
215 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
216 const ObjCInterfaceDecl *Interface,
217 const ObjCIvarDecl *Ivar) = 0;
218 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
219 llvm::Value *DestPtr,
221 llvm::Value *Size) = 0;
222 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
223 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &) = 0;
227 /// Creates an instance of an Objective-C runtime class.
228 //TODO: This should include some way of selecting which runtime to target.
229 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
230 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
231 CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);