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
;
61 // FIXME: Several methods should be pure virtual but aren't to avoid the
62 // partially-implemented subclass breaking.
64 /// Implements runtime-specific code generation functions.
67 // Utility functions for unified ivar access. These need to
68 // eventually be folded into other places (the structure layout
71 /// Compute an offset to the given ivar, suitable for passing to
72 /// EmitValueForIvarAtOffset. Note that the correct handling of
73 /// bit-fields is carefully coordinated by these two, use caution!
75 /// The latter overload is suitable for computing the offset of a
77 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule
&CGM
,
78 const ObjCInterfaceDecl
*OID
,
79 const ObjCIvarDecl
*Ivar
);
80 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule
&CGM
,
81 const ObjCImplementationDecl
*OID
,
82 const ObjCIvarDecl
*Ivar
);
84 LValue
EmitValueForIvarAtOffset(CodeGen::CodeGenFunction
&CGF
,
85 const ObjCInterfaceDecl
*OID
,
86 llvm::Value
*BaseValue
,
87 const ObjCIvarDecl
*Ivar
,
88 unsigned CVRQualifiers
,
92 virtual ~CGObjCRuntime();
94 /// Generate the function required to register all Objective-C components in
95 /// this compilation unit with the runtime library.
96 virtual llvm::Function
*ModuleInitFunction() = 0;
98 /// Get a selector for the specified name and type values. The
99 /// return value should have the LLVM type for pointer-to
100 /// ASTContext::getObjCSelType().
101 virtual llvm::Value
*GetSelector(CGBuilderTy
&Builder
,
102 Selector Sel
, bool lval
=false) = 0;
104 /// Get a typed selector.
105 virtual llvm::Value
*GetSelector(CGBuilderTy
&Builder
,
106 const ObjCMethodDecl
*Method
) = 0;
108 /// Get the type constant to catch for the given ObjC pointer type.
109 /// This is used externally to implement catching ObjC types in C++.
110 /// Runtimes which don't support this should add the appropriate
112 virtual llvm::Constant
*GetEHType(QualType T
) = 0;
114 /// Generate a constant string object.
115 virtual llvm::Constant
*GenerateConstantString(const StringLiteral
*) = 0;
117 /// Generate a category. A category contains a list of methods (and
118 /// accompanying metadata) and a list of protocols.
119 virtual void GenerateCategory(const ObjCCategoryImplDecl
*OCD
) = 0;
121 /// Generate a class stucture for this class.
122 virtual void GenerateClass(const ObjCImplementationDecl
*OID
) = 0;
124 /// Generate an Objective-C message send operation.
126 /// \param Method - The method being called, this may be null if synthesizing
127 /// a property setter or getter.
128 virtual CodeGen::RValue
129 GenerateMessageSend(CodeGen::CodeGenFunction
&CGF
,
130 ReturnValueSlot ReturnSlot
,
133 llvm::Value
*Receiver
,
134 const CallArgList
&CallArgs
,
135 const ObjCInterfaceDecl
*Class
= 0,
136 const ObjCMethodDecl
*Method
= 0) = 0;
138 /// Generate an Objective-C message send operation to the super
139 /// class initiated in a method for Class and with the given Self
142 /// \param Method - The method being called, this may be null if synthesizing
143 /// a property setter or getter.
144 virtual CodeGen::RValue
145 GenerateMessageSendSuper(CodeGen::CodeGenFunction
&CGF
,
146 ReturnValueSlot ReturnSlot
,
149 const ObjCInterfaceDecl
*Class
,
153 const CallArgList
&CallArgs
,
154 const ObjCMethodDecl
*Method
= 0) = 0;
156 /// Emit the code to return the named protocol as an object, as in a
157 /// @protocol expression.
158 virtual llvm::Value
*GenerateProtocolRef(CGBuilderTy
&Builder
,
159 const ObjCProtocolDecl
*OPD
) = 0;
161 /// Generate the named protocol. Protocols contain method metadata but no
163 virtual void GenerateProtocol(const ObjCProtocolDecl
*OPD
) = 0;
165 /// Generate a function preamble for a method with the specified
168 // FIXME: Current this just generates the Function definition, but really this
169 // should also be generating the loads of the parameters, as the runtime
170 // should have full control over how parameters are passed.
171 virtual llvm::Function
*GenerateMethod(const ObjCMethodDecl
*OMD
,
172 const ObjCContainerDecl
*CD
) = 0;
174 /// Return the runtime function for getting properties.
175 virtual llvm::Constant
*GetPropertyGetFunction() = 0;
177 /// Return the runtime function for setting properties.
178 virtual llvm::Constant
*GetPropertySetFunction() = 0;
180 // API for atomic copying of qualified aggregates in getter.
181 virtual llvm::Constant
*GetGetStructFunction() = 0;
182 // API for atomic copying of qualified aggregates in setter.
183 virtual llvm::Constant
*GetSetStructFunction() = 0;
185 /// GetClass - Return a reference to the class for the given
187 virtual llvm::Value
*GetClass(CGBuilderTy
&Builder
,
188 const ObjCInterfaceDecl
*OID
) = 0;
190 /// EnumerationMutationFunction - Return the function that's called by the
191 /// compiler when a mutation is detected during foreach iteration.
192 virtual llvm::Constant
*EnumerationMutationFunction() = 0;
194 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction
&CGF
,
195 const ObjCAtSynchronizedStmt
&S
) = 0;
196 virtual void EmitTryStmt(CodeGen::CodeGenFunction
&CGF
,
197 const ObjCAtTryStmt
&S
) = 0;
198 virtual void EmitThrowStmt(CodeGen::CodeGenFunction
&CGF
,
199 const ObjCAtThrowStmt
&S
) = 0;
200 virtual llvm::Value
*EmitObjCWeakRead(CodeGen::CodeGenFunction
&CGF
,
201 llvm::Value
*AddrWeakObj
) = 0;
202 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction
&CGF
,
203 llvm::Value
*src
, llvm::Value
*dest
) = 0;
204 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction
&CGF
,
205 llvm::Value
*src
, llvm::Value
*dest
,
206 bool threadlocal
=false) = 0;
207 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction
&CGF
,
208 llvm::Value
*src
, llvm::Value
*dest
,
209 llvm::Value
*ivarOffset
) = 0;
210 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction
&CGF
,
211 llvm::Value
*src
, llvm::Value
*dest
) = 0;
213 virtual LValue
EmitObjCValueForIvar(CodeGen::CodeGenFunction
&CGF
,
215 llvm::Value
*BaseValue
,
216 const ObjCIvarDecl
*Ivar
,
217 unsigned CVRQualifiers
) = 0;
218 virtual llvm::Value
*EmitIvarOffset(CodeGen::CodeGenFunction
&CGF
,
219 const ObjCInterfaceDecl
*Interface
,
220 const ObjCIvarDecl
*Ivar
) = 0;
221 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction
&CGF
,
222 llvm::Value
*DestPtr
,
224 llvm::Value
*Size
) = 0;
225 virtual llvm::Constant
*BuildGCBlockLayout(CodeGen::CodeGenModule
&CGM
,
226 const CodeGen::CGBlockInfo
&blockInfo
) = 0;
229 /// Creates an instance of an Objective-C runtime class.
230 //TODO: This should include some way of selecting which runtime to target.
231 CGObjCRuntime
*CreateGNUObjCRuntime(CodeGenModule
&CGM
);
232 CGObjCRuntime
*CreateMacObjCRuntime(CodeGenModule
&CGM
);
233 CGObjCRuntime
*CreateMacNonFragileABIObjCRuntime(CodeGenModule
&CGM
);