blob: 675ff340b64727896a8e5ca0e327db8d6c119cbc [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;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000042 class ObjCCategoryImplDecl;
43 class ObjCImplementationDecl;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000044 class ObjCInterfaceDecl;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000045 class ObjCMessageExpr;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000046 class ObjCMethodDecl;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000047 class ObjCProtocolDecl;
Chris Lattner8e67b632008-06-26 04:37:12 +000048 class Selector;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000049
Chris Lattner0f984262008-03-01 08:50:34 +000050namespace CodeGen {
Chris Lattnerdce14062008-06-26 04:19:03 +000051 class CodeGenModule;
Chris Lattner0f984262008-03-01 08:50:34 +000052
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000053//FIXME Several methods should be pure virtual but aren't to avoid the
54//partially-implemented subclass breaking.
55
56/// Implements runtime-specific code generation functions.
Chris Lattner0f984262008-03-01 08:50:34 +000057class CGObjCRuntime {
Daniel Dunbar58bf6102008-08-11 16:50:21 +000058
Chris Lattner0f984262008-03-01 08:50:34 +000059public:
60 virtual ~CGObjCRuntime();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000061
62 /// Generate the function required to register all Objective-C components in
63 /// this compilation unit with the runtime library.
64 virtual llvm::Function *ModuleInitFunction() = 0;
65
66 /// Get a selector for the specified name and type values. The
67 /// return value should have the LLVM type for pointer-to
68 /// ASTContext::getObjCSelType().
Daniel Dunbar45d196b2008-11-01 01:53:16 +000069 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000070 Selector Sel) = 0;
71
72 /// Generate a constant string object.
73 virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
74
75 /// Generate a category. A category contains a list of methods (and
76 /// accompanying metadata) and a list of protocols.
77 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
78
79 /// Generate a class stucture for this class.
80 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +000081
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000082 /// Generate an Objective-C message send operation.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000083 virtual CodeGen::RValue
84 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000085 QualType ResultType,
86 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000087 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000088 bool IsClassMessage,
89 const CallArgList &CallArgs) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000090
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000091 /// Generate an Objective-C message send operation to the super
Daniel Dunbarf56f1912008-08-25 08:19:24 +000092 /// class initiated in a method for Class and with the given Self
93 /// object.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000094 virtual CodeGen::RValue
95 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000096 QualType ResultType,
97 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000098 const ObjCInterfaceDecl *Class,
99 llvm::Value *Self,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000100 bool IsClassMessage,
101 const CallArgList &CallArgs) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000102
103 /// Emit the code to return the named protocol as an object, as in a
104 /// @protocol expression.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000105 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000106 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000107
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000108 /// Generate the named protocol. Protocols contain method metadata but no
109 /// implementations.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000110 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000111
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000112 /// Generate a function preamble for a method with the specified
113 /// types.
114
115 // FIXME: Current this just generates the Function definition, but
116 // really this should also be generating the loads of the
117 // parameters, as the runtime should have full control over how
118 // parameters are passed.
119 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000120
Daniel Dunbar49f66022008-09-24 03:38:44 +0000121 /// Return the runtime function for getting properties.
122 virtual llvm::Function *GetPropertyGetFunction() = 0;
123
124 /// Return the runtime function for setting properties.
125 virtual llvm::Function *GetPropertySetFunction() = 0;
126
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000127 /// GetClass - Return a reference to the class for the given
128 /// interface decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000129 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000130 const ObjCInterfaceDecl *OID) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000131
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000132 /// EnumerationMutationFunction - Return the function that's called by the
133 /// compiler when a mutation is detected during foreach iteration.
134 virtual llvm::Function *EnumerationMutationFunction() = 0;
135
Chris Lattner391d77a2008-03-30 23:03:07 +0000136 /// If instance variable addresses are determined at runtime then this should
137 /// return true, otherwise instance variables will be accessed directly from
138 /// the structure. If this returns true then @defs is invalid for this
139 /// runtime and a warning should be generated.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000140 virtual bool LateBoundIVars() const { return false; }
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000141
142 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
143 const ObjCAtTryStmt &S) = 0;
144 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
145 const ObjCAtThrowStmt &S) = 0;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000146 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
147 const ObjCAtSynchronizedStmt &S) = 0;
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000148 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000149 llvm::Value *AddrWeakObj) = 0;
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000150 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
151 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000152 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
153 llvm::Value *src, llvm::Value *dest) = 0;
154 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
155 llvm::Value *src, llvm::Value *dest) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +0000156};
157
Chris Lattner391d77a2008-03-30 23:03:07 +0000158/// Creates an instance of an Objective-C runtime class.
159//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000160CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
161CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000162}
163}
164#endif