blob: 7f030f2341dad022a04118b0830db50c678602c6 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
Chris Lattner0f984262008-03-01 08:50:34 +00002//
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
Daniel Dunbar45d196b2008-11-01 01:53:16 +000018#include "CGBuilder.h"
Daniel Dunbar46f45b92008-09-09 01:06:48 +000019#include "CGCall.h"
Daniel Dunbar45d196b2008-11-01 01:53:16 +000020#include "CGValue.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/AST/DeclObjC.h"
22#include "clang/Basic/IdentifierTable.h" // Selector
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000023
Chris Lattner0f984262008-03-01 08:50:34 +000024namespace llvm {
Chris Lattner0f984262008-03-01 08:50:34 +000025 class Constant;
Daniel Dunbar97776872009-04-22 07:32:20 +000026 class Function;
27 class Module;
28 class StructLayout;
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000029 class StructType;
Chris Lattner0f984262008-03-01 08:50:34 +000030 class Type;
31 class Value;
Chris Lattner0f984262008-03-01 08:50:34 +000032}
33
34namespace clang {
Daniel Dunbar46f45b92008-09-09 01:06:48 +000035namespace CodeGen {
36 class CodeGenFunction;
37}
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000038
Daniel Dunbar97776872009-04-22 07:32:20 +000039 class FieldDecl;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +000040 class ObjCAtTryStmt;
41 class ObjCAtThrowStmt;
Chris Lattner10cac6f2008-11-15 21:26:17 +000042 class ObjCAtSynchronizedStmt;
Fariborz Jahanian679a5022009-01-10 21:06:09 +000043 class ObjCContainerDecl;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000044 class ObjCCategoryImplDecl;
45 class ObjCImplementationDecl;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000046 class ObjCInterfaceDecl;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000047 class ObjCMessageExpr;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000048 class ObjCMethodDecl;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000049 class ObjCProtocolDecl;
Chris Lattner8e67b632008-06-26 04:37:12 +000050 class Selector;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +000051 class ObjCIvarDecl;
Steve Naroff33fdb732009-03-31 16:53:37 +000052 class ObjCStringLiteral;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +000053 class BlockDeclRefExpr;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000054
Chris Lattner0f984262008-03-01 08:50:34 +000055namespace CodeGen {
Chris Lattnerdce14062008-06-26 04:19:03 +000056 class CodeGenModule;
John McCall6b5a61b2011-02-07 10:33:21 +000057 class CGBlockInfo;
Chris Lattner0f984262008-03-01 08:50:34 +000058
Mike Stumpf5408fe2009-05-16 07:57:57 +000059// FIXME: Several methods should be pure virtual but aren't to avoid the
60// partially-implemented subclass breaking.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000061
62/// Implements runtime-specific code generation functions.
Chris Lattner0f984262008-03-01 08:50:34 +000063class CGObjCRuntime {
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +000064protected:
John McCallde5d3c72012-02-17 03:33:10 +000065 CodeGen::CodeGenModule &CGM;
66 CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {}
67
Daniel Dunbar97776872009-04-22 07:32:20 +000068 // Utility functions for unified ivar access. These need to
69 // eventually be folded into other places (the structure layout
70 // code).
71
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000072 /// Compute an offset to the given ivar, suitable for passing to
73 /// EmitValueForIvarAtOffset. Note that the correct handling of
74 /// bit-fields is carefully coordinated by these two, use caution!
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000075 ///
76 /// The latter overload is suitable for computing the offset of a
77 /// sythesized ivar.
Eli Friedmane5b46662012-11-06 22:15:52 +000078 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
79 const ObjCInterfaceDecl *OID,
80 const ObjCIvarDecl *Ivar);
81 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
82 const ObjCImplementationDecl *OID,
83 const ObjCIvarDecl *Ivar);
Daniel Dunbar97776872009-04-22 07:32:20 +000084
85 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
86 const ObjCInterfaceDecl *OID,
87 llvm::Value *BaseValue,
88 const ObjCIvarDecl *Ivar,
89 unsigned CVRQualifiers,
Mike Stump1eb44332009-09-09 15:08:12 +000090 llvm::Value *Offset);
David Chisnall9735ca62011-03-25 11:57:33 +000091 /// Emits a try / catch statement. This function is intended to be called by
92 /// subclasses, and provides a generic mechanism for generating these, which
James Dennett2ee5ba32012-06-15 22:10:14 +000093 /// should be usable by all runtimes. The caller must provide the functions
94 /// to call when entering and exiting a \@catch() block, and the function
95 /// used to rethrow exceptions. If the begin and end catch functions are
96 /// NULL, then the function assumes that the EH personality function provides
97 /// the thrown object directly.
David Chisnall9735ca62011-03-25 11:57:33 +000098 void EmitTryCatchStmt(CodeGenFunction &CGF,
99 const ObjCAtTryStmt &S,
David Chisnall789ecde2011-05-23 22:33:28 +0000100 llvm::Constant *beginCatchFn,
101 llvm::Constant *endCatchFn,
102 llvm::Constant *exceptionRethrowFn);
James Dennett2ee5ba32012-06-15 22:10:14 +0000103 /// Emits an \@synchronize() statement, using the \p syncEnterFn and
104 /// \p syncExitFn arguments as the functions called to lock and unlock
105 /// the object. This function can be called by subclasses that use
106 /// zero-cost exception handling.
David Chisnall9735ca62011-03-25 11:57:33 +0000107 void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
108 const ObjCAtSynchronizedStmt &S,
109 llvm::Function *syncEnterFn,
110 llvm::Function *syncExitFn);
Daniel Dunbar58bf6102008-08-11 16:50:21 +0000111
Chris Lattner0f984262008-03-01 08:50:34 +0000112public:
113 virtual ~CGObjCRuntime();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000114
115 /// Generate the function required to register all Objective-C components in
116 /// this compilation unit with the runtime library.
117 virtual llvm::Function *ModuleInitFunction() = 0;
118
119 /// Get a selector for the specified name and type values. The
120 /// return value should have the LLVM type for pointer-to
121 /// ASTContext::getObjCSelType().
John McCallbd7370a2013-02-28 19:01:20 +0000122 virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
Fariborz Jahanian03b29602010-06-17 19:56:20 +0000123 Selector Sel, bool lval=false) = 0;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000124
Mike Stump1eb44332009-09-09 15:08:12 +0000125 /// Get a typed selector.
John McCallbd7370a2013-02-28 19:01:20 +0000126 virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000127 const ObjCMethodDecl *Method) = 0;
128
John McCall5a180392010-07-24 00:37:23 +0000129 /// Get the type constant to catch for the given ObjC pointer type.
130 /// This is used externally to implement catching ObjC types in C++.
131 /// Runtimes which don't support this should add the appropriate
132 /// error to Sema.
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +0000133 virtual llvm::Constant *GetEHType(QualType T) = 0;
John McCall5a180392010-07-24 00:37:23 +0000134
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000135 /// Generate a constant string object.
David Chisnall0d13f6f2010-01-23 02:40:42 +0000136 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000137
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000138 /// Generate a category. A category contains a list of methods (and
139 /// accompanying metadata) and a list of protocols.
140 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
141
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000142 /// Generate a class structure for this class.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000143 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000144
David Chisnall29254f42012-01-31 18:59:20 +0000145 /// Register an class alias.
146 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
147
Mike Stump1eb44332009-09-09 15:08:12 +0000148 /// Generate an Objective-C message send operation.
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000149 ///
150 /// \param Method - The method being called, this may be null if synthesizing
151 /// a property setter or getter.
Mike Stump1eb44332009-09-09 15:08:12 +0000152 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000153 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000154 ReturnValueSlot ReturnSlot,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000155 QualType ResultType,
156 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000157 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000158 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000159 const ObjCInterfaceDecl *Class = 0,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000160 const ObjCMethodDecl *Method = 0) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000161
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000162 /// Generate an Objective-C message send operation to the super
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000163 /// class initiated in a method for Class and with the given Self
164 /// object.
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000165 ///
166 /// \param Method - The method being called, this may be null if synthesizing
167 /// a property setter or getter.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000168 virtual CodeGen::RValue
169 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000170 ReturnValueSlot ReturnSlot,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000171 QualType ResultType,
172 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000173 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000174 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000175 llvm::Value *Self,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000176 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000177 const CallArgList &CallArgs,
178 const ObjCMethodDecl *Method = 0) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000179
180 /// Emit the code to return the named protocol as an object, as in a
James Dennett17d26a62012-06-11 06:19:40 +0000181 /// \@protocol expression.
John McCallbd7370a2013-02-28 19:01:20 +0000182 virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000183 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000184
Mike Stump1eb44332009-09-09 15:08:12 +0000185 /// Generate the named protocol. Protocols contain method metadata but no
186 /// implementations.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000187 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000188
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000189 /// Generate a function preamble for a method with the specified
Mike Stump1eb44332009-09-09 15:08:12 +0000190 /// types.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000191
Mike Stumpf5408fe2009-05-16 07:57:57 +0000192 // FIXME: Current this just generates the Function definition, but really this
193 // should also be generating the loads of the parameters, as the runtime
194 // should have full control over how parameters are passed.
Mike Stump1eb44332009-09-09 15:08:12 +0000195 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000196 const ObjCContainerDecl *CD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000197
Daniel Dunbar49f66022008-09-24 03:38:44 +0000198 /// Return the runtime function for getting properties.
Chris Lattner74391b42009-03-22 21:03:39 +0000199 virtual llvm::Constant *GetPropertyGetFunction() = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Daniel Dunbar49f66022008-09-24 03:38:44 +0000201 /// Return the runtime function for setting properties.
Chris Lattner74391b42009-03-22 21:03:39 +0000202 virtual llvm::Constant *GetPropertySetFunction() = 0;
Daniel Dunbar49f66022008-09-24 03:38:44 +0000203
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000204 /// Return the runtime function for optimized setting properties.
205 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
206 bool copy) = 0;
207
David Chisnall8fac25d2010-12-26 22:13:16 +0000208 // API for atomic copying of qualified aggregates in getter.
209 virtual llvm::Constant *GetGetStructFunction() = 0;
210 // API for atomic copying of qualified aggregates in setter.
211 virtual llvm::Constant *GetSetStructFunction() = 0;
David Chisnalld397cfe2012-12-17 18:54:24 +0000212 /// API for atomic copying of qualified aggregates with non-trivial copy
213 /// assignment (c++) in setter.
214 virtual llvm::Constant *GetCppAtomicObjectSetFunction() = 0;
215 /// API for atomic copying of qualified aggregates with non-trivial copy
216 /// assignment (c++) in getter.
217 virtual llvm::Constant *GetCppAtomicObjectGetFunction() = 0;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000218
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000219 /// GetClass - Return a reference to the class for the given
220 /// interface decl.
John McCallbd7370a2013-02-28 19:01:20 +0000221 virtual llvm::Value *GetClass(CodeGenFunction &CGF,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000222 const ObjCInterfaceDecl *OID) = 0;
John McCallf85e1932011-06-15 23:02:42 +0000223
224
John McCallbd7370a2013-02-28 19:01:20 +0000225 virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000226 llvm_unreachable("autoreleasepool unsupported in this ABI");
John McCallf85e1932011-06-15 23:02:42 +0000227 }
228
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000229 /// EnumerationMutationFunction - Return the function that's called by the
230 /// compiler when a mutation is detected during foreach iteration.
Chris Lattner74391b42009-03-22 21:03:39 +0000231 virtual llvm::Constant *EnumerationMutationFunction() = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
John McCallf1549f62010-07-06 01:34:17 +0000233 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
234 const ObjCAtSynchronizedStmt &S) = 0;
235 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
236 const ObjCAtTryStmt &S) = 0;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000237 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6a3c70e2013-01-10 19:02:56 +0000238 const ObjCAtThrowStmt &S,
239 bool ClearInsertionPoint=true) = 0;
Daniel Dunbar6bf2ae02009-04-21 00:49:20 +0000240 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
241 llvm::Value *AddrWeakObj) = 0;
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000242 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
243 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000244 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000245 llvm::Value *src, llvm::Value *dest,
246 bool threadlocal=false) = 0;
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000247 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000248 llvm::Value *src, llvm::Value *dest,
249 llvm::Value *ivarOffset) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000250 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
251 llvm::Value *src, llvm::Value *dest) = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000253 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
254 QualType ObjectTy,
255 llvm::Value *BaseValue,
256 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000257 unsigned CVRQualifiers) = 0;
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000258 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000259 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000260 const ObjCIvarDecl *Ivar) = 0;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000261 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +0000262 llvm::Value *DestPtr,
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000263 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000264 llvm::Value *Size) = 0;
John McCall6b5a61b2011-02-07 10:33:21 +0000265 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
266 const CodeGen::CGBlockInfo &blockInfo) = 0;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +0000267 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
268 const CodeGen::CGBlockInfo &blockInfo) = 0;
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000269 virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
270 QualType T) = 0;
Fariborz Jahanian6f40e222011-05-17 22:21:16 +0000271 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) = 0;
John McCallde5d3c72012-02-17 03:33:10 +0000272
273 struct MessageSendInfo {
274 const CGFunctionInfo &CallInfo;
275 llvm::PointerType *MessengerType;
276
277 MessageSendInfo(const CGFunctionInfo &callInfo,
278 llvm::PointerType *messengerType)
279 : CallInfo(callInfo), MessengerType(messengerType) {}
280 };
281
282 MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
283 QualType resultType,
284 CallArgList &callArgs);
Eli Friedman77457862012-11-06 23:40:48 +0000285
286 // FIXME: This probably shouldn't be here, but the code to compute
287 // it is here.
288 unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,
289 const ObjCInterfaceDecl *ID,
290 const ObjCIvarDecl *Ivar);
Chris Lattner0f984262008-03-01 08:50:34 +0000291};
292
Mike Stump1eb44332009-09-09 15:08:12 +0000293/// Creates an instance of an Objective-C runtime class.
Chris Lattner391d77a2008-03-30 23:03:07 +0000294//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000295CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
296CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000297}
298}
299#endif