blob: 9778e18fce783657c9ac7d37951af43881696982 [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 Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/Basic/IdentifierTable.h" // Selector
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +000019#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidis8ef07c02008-06-01 21:23:24 +000020#include <string>
Chris Lattner0f984262008-03-01 08:50:34 +000021
Daniel Dunbar45d196b2008-11-01 01:53:16 +000022#include "CGBuilder.h"
Daniel Dunbar46f45b92008-09-09 01:06:48 +000023#include "CGCall.h"
Daniel Dunbar45d196b2008-11-01 01:53:16 +000024#include "CGValue.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000025
Chris Lattner0f984262008-03-01 08:50:34 +000026namespace llvm {
Chris Lattner0f984262008-03-01 08:50:34 +000027 class Constant;
Daniel Dunbar97776872009-04-22 07:32:20 +000028 class Function;
29 class Module;
30 class StructLayout;
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000031 class StructType;
Chris Lattner0f984262008-03-01 08:50:34 +000032 class Type;
33 class Value;
Chris Lattner0f984262008-03-01 08:50:34 +000034}
35
36namespace clang {
Daniel Dunbar46f45b92008-09-09 01:06:48 +000037namespace CodeGen {
38 class CodeGenFunction;
39}
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000040
Daniel Dunbar97776872009-04-22 07:32:20 +000041 class FieldDecl;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +000042 class ObjCAtTryStmt;
43 class ObjCAtThrowStmt;
Chris Lattner10cac6f2008-11-15 21:26:17 +000044 class ObjCAtSynchronizedStmt;
Fariborz Jahanian679a5022009-01-10 21:06:09 +000045 class ObjCContainerDecl;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000046 class ObjCCategoryImplDecl;
47 class ObjCImplementationDecl;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000048 class ObjCInterfaceDecl;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000049 class ObjCMessageExpr;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000050 class ObjCMethodDecl;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000051 class ObjCProtocolDecl;
Chris Lattner8e67b632008-06-26 04:37:12 +000052 class Selector;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +000053 class ObjCIvarDecl;
Steve Naroff33fdb732009-03-31 16:53:37 +000054 class ObjCStringLiteral;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +000055 class BlockDeclRefExpr;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000056
Chris Lattner0f984262008-03-01 08:50:34 +000057namespace CodeGen {
Chris Lattnerdce14062008-06-26 04:19:03 +000058 class CodeGenModule;
John McCall6b5a61b2011-02-07 10:33:21 +000059 class CGBlockInfo;
Chris Lattner0f984262008-03-01 08:50:34 +000060
Mike Stumpf5408fe2009-05-16 07:57:57 +000061// FIXME: Several methods should be pure virtual but aren't to avoid the
62// partially-implemented subclass breaking.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000063
64/// Implements runtime-specific code generation functions.
Chris Lattner0f984262008-03-01 08:50:34 +000065class CGObjCRuntime {
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +000066protected:
Daniel Dunbar97776872009-04-22 07:32:20 +000067 // Utility functions for unified ivar access. These need to
68 // eventually be folded into other places (the structure layout
69 // code).
70
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000071 /// 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!
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000074 ///
75 /// The latter overload is suitable for computing the offset of a
76 /// sythesized ivar.
Daniel Dunbar97776872009-04-22 07:32:20 +000077 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
78 const ObjCInterfaceDecl *OID,
79 const ObjCIvarDecl *Ivar);
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000080 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
81 const ObjCImplementationDecl *OID,
82 const ObjCIvarDecl *Ivar);
Daniel Dunbar97776872009-04-22 07:32:20 +000083
84 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
85 const ObjCInterfaceDecl *OID,
86 llvm::Value *BaseValue,
87 const ObjCIvarDecl *Ivar,
88 unsigned CVRQualifiers,
Mike Stump1eb44332009-09-09 15:08:12 +000089 llvm::Value *Offset);
David Chisnall9735ca62011-03-25 11:57:33 +000090 /// Emits a try / catch statement. This function is intended to be called by
91 /// subclasses, and provides a generic mechanism for generating these, which
92 /// should be usable by all runtimes. The caller must provide the functions to
93 /// call when entering and exiting a @catch() block, and the function used to
94 /// rethrow exceptions. If the begin and end catch functions are NULL, then
95 /// the function assumes that the EH personality function provides the
96 /// thrown object directly.
97 void EmitTryCatchStmt(CodeGenFunction &CGF,
98 const ObjCAtTryStmt &S,
99 llvm::Function *beginCatchFn,
100 llvm::Function *endCatchFn,
101 llvm::Function *exceptionRethrowFn);
102 /// Emits an @synchronize() statement, using the syncEnterFn and syncExitFn
103 /// arguments as the functions called to lock and unlock the object. This
104 /// function can be called by subclasses that use zero-cost exception
105 /// handling.
106 void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
107 const ObjCAtSynchronizedStmt &S,
108 llvm::Function *syncEnterFn,
109 llvm::Function *syncExitFn);
Daniel Dunbar58bf6102008-08-11 16:50:21 +0000110
Chris Lattner0f984262008-03-01 08:50:34 +0000111public:
112 virtual ~CGObjCRuntime();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000113
114 /// Generate the function required to register all Objective-C components in
115 /// this compilation unit with the runtime library.
116 virtual llvm::Function *ModuleInitFunction() = 0;
117
118 /// Get a selector for the specified name and type values. The
119 /// return value should have the LLVM type for pointer-to
120 /// ASTContext::getObjCSelType().
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000121 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +0000122 Selector Sel, bool lval=false) = 0;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000123
Mike Stump1eb44332009-09-09 15:08:12 +0000124 /// Get a typed selector.
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000125 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
126 const ObjCMethodDecl *Method) = 0;
127
John McCall5a180392010-07-24 00:37:23 +0000128 /// Get the type constant to catch for the given ObjC pointer type.
129 /// This is used externally to implement catching ObjC types in C++.
130 /// Runtimes which don't support this should add the appropriate
131 /// error to Sema.
132 virtual llvm::Constant *GetEHType(QualType T) = 0;
133
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000134 /// Generate a constant string object.
David Chisnall0d13f6f2010-01-23 02:40:42 +0000135 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000136
137 /// Generate a category. A category contains a list of methods (and
138 /// accompanying metadata) and a list of protocols.
139 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
140
141 /// Generate a class stucture for this class.
142 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
144 /// Generate an Objective-C message send operation.
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000145 ///
146 /// \param Method - The method being called, this may be null if synthesizing
147 /// a property setter or getter.
Mike Stump1eb44332009-09-09 15:08:12 +0000148 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000149 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000150 ReturnValueSlot ReturnSlot,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000151 QualType ResultType,
152 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000153 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000154 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000155 const ObjCInterfaceDecl *Class = 0,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000156 const ObjCMethodDecl *Method = 0) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000157
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000158 /// Generate an Objective-C message send operation to the super
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000159 /// class initiated in a method for Class and with the given Self
160 /// object.
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000161 ///
162 /// \param Method - The method being called, this may be null if synthesizing
163 /// a property setter or getter.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000164 virtual CodeGen::RValue
165 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000166 ReturnValueSlot ReturnSlot,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000167 QualType ResultType,
168 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000169 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000170 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000171 llvm::Value *Self,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000172 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000173 const CallArgList &CallArgs,
174 const ObjCMethodDecl *Method = 0) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000175
176 /// Emit the code to return the named protocol as an object, as in a
177 /// @protocol expression.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000178 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000179 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000180
Mike Stump1eb44332009-09-09 15:08:12 +0000181 /// Generate the named protocol. Protocols contain method metadata but no
182 /// implementations.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000183 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000184
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000185 /// Generate a function preamble for a method with the specified
Mike Stump1eb44332009-09-09 15:08:12 +0000186 /// types.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000187
Mike Stumpf5408fe2009-05-16 07:57:57 +0000188 // FIXME: Current this just generates the Function definition, but really this
189 // should also be generating the loads of the parameters, as the runtime
190 // should have full control over how parameters are passed.
Mike Stump1eb44332009-09-09 15:08:12 +0000191 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000192 const ObjCContainerDecl *CD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000193
Daniel Dunbar49f66022008-09-24 03:38:44 +0000194 /// Return the runtime function for getting properties.
Chris Lattner74391b42009-03-22 21:03:39 +0000195 virtual llvm::Constant *GetPropertyGetFunction() = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Daniel Dunbar49f66022008-09-24 03:38:44 +0000197 /// Return the runtime function for setting properties.
Chris Lattner74391b42009-03-22 21:03:39 +0000198 virtual llvm::Constant *GetPropertySetFunction() = 0;
Daniel Dunbar49f66022008-09-24 03:38:44 +0000199
David Chisnall8fac25d2010-12-26 22:13:16 +0000200 // API for atomic copying of qualified aggregates in getter.
201 virtual llvm::Constant *GetGetStructFunction() = 0;
202 // API for atomic copying of qualified aggregates in setter.
203 virtual llvm::Constant *GetSetStructFunction() = 0;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000204
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000205 /// GetClass - Return a reference to the class for the given
206 /// interface decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000207 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000208 const ObjCInterfaceDecl *OID) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000209
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000210 /// EnumerationMutationFunction - Return the function that's called by the
211 /// compiler when a mutation is detected during foreach iteration.
Chris Lattner74391b42009-03-22 21:03:39 +0000212 virtual llvm::Constant *EnumerationMutationFunction() = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
John McCallf1549f62010-07-06 01:34:17 +0000214 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
215 const ObjCAtSynchronizedStmt &S) = 0;
216 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
217 const ObjCAtTryStmt &S) = 0;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000218 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
219 const ObjCAtThrowStmt &S) = 0;
Daniel Dunbar6bf2ae02009-04-21 00:49:20 +0000220 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
221 llvm::Value *AddrWeakObj) = 0;
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000222 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
223 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000224 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000225 llvm::Value *src, llvm::Value *dest,
226 bool threadlocal=false) = 0;
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000227 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000228 llvm::Value *src, llvm::Value *dest,
229 llvm::Value *ivarOffset) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000230 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
231 llvm::Value *src, llvm::Value *dest) = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000233 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
234 QualType ObjectTy,
235 llvm::Value *BaseValue,
236 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000237 unsigned CVRQualifiers) = 0;
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000238 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000239 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000240 const ObjCIvarDecl *Ivar) = 0;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000241 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +0000242 llvm::Value *DestPtr,
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000243 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000244 llvm::Value *Size) = 0;
John McCall6b5a61b2011-02-07 10:33:21 +0000245 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
246 const CodeGen::CGBlockInfo &blockInfo) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +0000247};
248
Mike Stump1eb44332009-09-09 15:08:12 +0000249/// Creates an instance of an Objective-C runtime class.
Chris Lattner391d77a2008-03-30 23:03:07 +0000250//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000251CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
252CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000253}
254}
255#endif