blob: cae32edf04f2a93ca894fc836a0f4dbda6c04170 [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"
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;
28 class Type;
29 class Value;
30 class Module;
Chris Lattner391d77a2008-03-30 23:03:07 +000031 class Function;
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
Anders Carlsson64d5d6c2008-09-09 10:04:29 +000039 class ObjCAtTryStmt;
40 class ObjCAtThrowStmt;
Chris Lattner10cac6f2008-11-15 21:26:17 +000041 class ObjCAtSynchronizedStmt;
Fariborz Jahanian679a5022009-01-10 21:06:09 +000042 class ObjCContainerDecl;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000043 class ObjCCategoryImplDecl;
44 class ObjCImplementationDecl;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000045 class ObjCInterfaceDecl;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000046 class ObjCMessageExpr;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000047 class ObjCMethodDecl;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000048 class ObjCProtocolDecl;
Chris Lattner8e67b632008-06-26 04:37:12 +000049 class Selector;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +000050 class ObjCIvarDecl;
Steve Naroff33fdb732009-03-31 16:53:37 +000051 class ObjCStringLiteral;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000052
Chris Lattner0f984262008-03-01 08:50:34 +000053namespace CodeGen {
Chris Lattnerdce14062008-06-26 04:19:03 +000054 class CodeGenModule;
Chris Lattner0f984262008-03-01 08:50:34 +000055
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000056//FIXME Several methods should be pure virtual but aren't to avoid the
57//partially-implemented subclass breaking.
58
59/// Implements runtime-specific code generation functions.
Chris Lattner0f984262008-03-01 08:50:34 +000060class CGObjCRuntime {
Daniel Dunbar58bf6102008-08-11 16:50:21 +000061
Chris Lattner0f984262008-03-01 08:50:34 +000062public:
63 virtual ~CGObjCRuntime();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000064
65 /// Generate the function required to register all Objective-C components in
66 /// this compilation unit with the runtime library.
67 virtual llvm::Function *ModuleInitFunction() = 0;
68
69 /// Get a selector for the specified name and type values. The
70 /// return value should have the LLVM type for pointer-to
71 /// ASTContext::getObjCSelType().
Daniel Dunbar45d196b2008-11-01 01:53:16 +000072 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000073 Selector Sel) = 0;
74
75 /// Generate a constant string object.
Steve Naroff33fdb732009-03-31 16:53:37 +000076 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000077
78 /// Generate a category. A category contains a list of methods (and
79 /// accompanying metadata) and a list of protocols.
80 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
81
82 /// Generate a class stucture for this class.
83 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +000084
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000085 /// Generate an Objective-C message send operation.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000086 virtual CodeGen::RValue
87 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000088 QualType ResultType,
89 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000090 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000091 bool IsClassMessage,
92 const CallArgList &CallArgs) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000093
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000094 /// Generate an Objective-C message send operation to the super
Daniel Dunbarf56f1912008-08-25 08:19:24 +000095 /// class initiated in a method for Class and with the given Self
96 /// object.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000097 virtual CodeGen::RValue
98 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000099 QualType ResultType,
100 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000101 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000102 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000103 llvm::Value *Self,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000104 bool IsClassMessage,
105 const CallArgList &CallArgs) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000106
107 /// Emit the code to return the named protocol as an object, as in a
108 /// @protocol expression.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000109 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000110 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000111
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000112 /// Generate the named protocol. Protocols contain method metadata but no
113 /// implementations.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000114 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000115
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000116 /// Generate a function preamble for a method with the specified
117 /// types.
118
119 // FIXME: Current this just generates the Function definition, but
120 // really this should also be generating the loads of the
121 // parameters, as the runtime should have full control over how
122 // parameters are passed.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000123 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
124 const ObjCContainerDecl *CD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000125
Daniel Dunbar49f66022008-09-24 03:38:44 +0000126 /// Return the runtime function for getting properties.
Chris Lattner74391b42009-03-22 21:03:39 +0000127 virtual llvm::Constant *GetPropertyGetFunction() = 0;
Daniel Dunbar49f66022008-09-24 03:38:44 +0000128
129 /// Return the runtime function for setting properties.
Chris Lattner74391b42009-03-22 21:03:39 +0000130 virtual llvm::Constant *GetPropertySetFunction() = 0;
Daniel Dunbar49f66022008-09-24 03:38:44 +0000131
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000132 /// GetClass - Return a reference to the class for the given
133 /// interface decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000134 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000135 const ObjCInterfaceDecl *OID) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000136
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000137 /// EnumerationMutationFunction - Return the function that's called by the
138 /// compiler when a mutation is detected during foreach iteration.
Chris Lattner74391b42009-03-22 21:03:39 +0000139 virtual llvm::Constant *EnumerationMutationFunction() = 0;
Daniel Dunbar6bf2ae02009-04-21 00:49:20 +0000140
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000141 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
142 const Stmt &S) = 0;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000143 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
144 const ObjCAtThrowStmt &S) = 0;
Daniel Dunbar6bf2ae02009-04-21 00:49:20 +0000145 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
146 llvm::Value *AddrWeakObj) = 0;
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000147 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
148 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000149 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
150 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000151 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
152 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000153 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
154 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000155
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000156 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
157 QualType ObjectTy,
158 llvm::Value *BaseValue,
159 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000160 unsigned CVRQualifiers) = 0;
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000161 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000162 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000163 const ObjCIvarDecl *Ivar) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +0000164};
165
Chris Lattner391d77a2008-03-30 23:03:07 +0000166/// Creates an instance of an Objective-C runtime class.
167//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000168CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
169CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000170CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000171}
172}
173#endif