blob: 0f9cf0606d36e7dda1e966495aa2035cf373e5b3 [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
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000019#include "llvm/ADT/SmallVector.h"
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +000020#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidis8ef07c02008-06-01 21:23:24 +000021#include <string>
Chris Lattner0f984262008-03-01 08:50:34 +000022
Daniel Dunbar45d196b2008-11-01 01:53:16 +000023#include "CGBuilder.h"
Daniel Dunbar46f45b92008-09-09 01:06:48 +000024#include "CGCall.h"
Daniel Dunbar45d196b2008-11-01 01:53:16 +000025#include "CGValue.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000026
Chris Lattner0f984262008-03-01 08:50:34 +000027namespace llvm {
Chris Lattner0f984262008-03-01 08:50:34 +000028 class Constant;
Daniel Dunbar97776872009-04-22 07:32:20 +000029 class Function;
30 class Module;
31 class StructLayout;
Daniel Dunbar84ad77a2009-04-22 09:39:34 +000032 class StructType;
Chris Lattner0f984262008-03-01 08:50:34 +000033 class Type;
34 class Value;
Chris Lattner0f984262008-03-01 08:50:34 +000035}
36
37namespace clang {
Daniel Dunbar46f45b92008-09-09 01:06:48 +000038namespace CodeGen {
39 class CodeGenFunction;
40}
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000041
Daniel Dunbar97776872009-04-22 07:32:20 +000042 class FieldDecl;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +000043 class ObjCAtTryStmt;
44 class ObjCAtThrowStmt;
Chris Lattner10cac6f2008-11-15 21:26:17 +000045 class ObjCAtSynchronizedStmt;
Fariborz Jahanian679a5022009-01-10 21:06:09 +000046 class ObjCContainerDecl;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000047 class ObjCCategoryImplDecl;
48 class ObjCImplementationDecl;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000049 class ObjCInterfaceDecl;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000050 class ObjCMessageExpr;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000051 class ObjCMethodDecl;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000052 class ObjCProtocolDecl;
Chris Lattner8e67b632008-06-26 04:37:12 +000053 class Selector;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +000054 class ObjCIvarDecl;
Steve Naroff33fdb732009-03-31 16:53:37 +000055 class ObjCStringLiteral;
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;
Chris Lattner0f984262008-03-01 08:50:34 +000059
Mike Stumpf5408fe2009-05-16 07:57:57 +000060// FIXME: Several methods should be pure virtual but aren't to avoid the
61// partially-implemented subclass breaking.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000062
63/// Implements runtime-specific code generation functions.
Chris Lattner0f984262008-03-01 08:50:34 +000064class CGObjCRuntime {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000065public:
Daniel Dunbar97776872009-04-22 07:32:20 +000066 // Utility functions for unified ivar access. These need to
67 // eventually be folded into other places (the structure layout
68 // code).
69
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000070protected:
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,
89 llvm::Value *Offset);
Daniel Dunbar58bf6102008-08-11 16:50:21 +000090
Chris Lattner0f984262008-03-01 08:50:34 +000091public:
92 virtual ~CGObjCRuntime();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000093
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;
97
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +000098 /// Add metadata globals to the 'used' globals for final output.
99 virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray) = 0;
100
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000101 /// Get a selector for the specified name and type values. The
102 /// return value should have the LLVM type for pointer-to
103 /// ASTContext::getObjCSelType().
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000104 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000105 Selector Sel) = 0;
106
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000107 /// Get a typed selector.
108 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
109 const ObjCMethodDecl *Method) = 0;
110
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000111 /// Generate a constant string object.
Steve Naroff33fdb732009-03-31 16:53:37 +0000112 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000113
114 /// Generate a category. A category contains a list of methods (and
115 /// accompanying metadata) and a list of protocols.
116 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
117
118 /// Generate a class stucture for this class.
119 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +0000120
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000121 /// Generate an Objective-C message send operation.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000122 virtual CodeGen::RValue
123 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000124 QualType ResultType,
125 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000126 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000127 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000128 const CallArgList &CallArgs,
129 const ObjCMethodDecl *Method=0) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000130
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000131 /// Generate an Objective-C message send operation to the super
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000132 /// class initiated in a method for Class and with the given Self
133 /// object.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000134 virtual CodeGen::RValue
135 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000136 QualType ResultType,
137 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000138 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000139 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000140 llvm::Value *Self,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000141 bool IsClassMessage,
142 const CallArgList &CallArgs) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000143
144 /// Emit the code to return the named protocol as an object, as in a
145 /// @protocol expression.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000146 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000147 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000148
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000149 /// Generate the named protocol. Protocols contain method metadata but no
150 /// implementations.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000151 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000152
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000153 /// Generate a function preamble for a method with the specified
154 /// types.
155
Mike Stumpf5408fe2009-05-16 07:57:57 +0000156 // FIXME: Current this just generates the Function definition, but really this
157 // should also be generating the loads of the parameters, as the runtime
158 // should have full control over how parameters are passed.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000159 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
160 const ObjCContainerDecl *CD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000161
Daniel Dunbar49f66022008-09-24 03:38:44 +0000162 /// Return the runtime function for getting properties.
Chris Lattner74391b42009-03-22 21:03:39 +0000163 virtual llvm::Constant *GetPropertyGetFunction() = 0;
Daniel Dunbar49f66022008-09-24 03:38:44 +0000164
165 /// Return the runtime function for setting properties.
Chris Lattner74391b42009-03-22 21:03:39 +0000166 virtual llvm::Constant *GetPropertySetFunction() = 0;
Daniel Dunbar49f66022008-09-24 03:38:44 +0000167
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000168 /// GetClass - Return a reference to the class for the given
169 /// interface decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000170 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000171 const ObjCInterfaceDecl *OID) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000172
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000173 /// EnumerationMutationFunction - Return the function that's called by the
174 /// compiler when a mutation is detected during foreach iteration.
Chris Lattner74391b42009-03-22 21:03:39 +0000175 virtual llvm::Constant *EnumerationMutationFunction() = 0;
Daniel Dunbar6bf2ae02009-04-21 00:49:20 +0000176
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000177 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
178 const Stmt &S) = 0;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000179 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
180 const ObjCAtThrowStmt &S) = 0;
Daniel Dunbar6bf2ae02009-04-21 00:49:20 +0000181 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
182 llvm::Value *AddrWeakObj) = 0;
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000183 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
184 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000185 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
186 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000187 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
188 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000189 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
190 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000191
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000192 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
193 QualType ObjectTy,
194 llvm::Value *BaseValue,
195 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000196 unsigned CVRQualifiers) = 0;
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000197 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000198 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000199 const ObjCIvarDecl *Ivar) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +0000200};
201
Chris Lattner391d77a2008-03-30 23:03:07 +0000202/// Creates an instance of an Objective-C runtime class.
203//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000204CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
205CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000206CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000207}
208}
209#endif