blob: 409ae3722115d841a561eaea1af20824e9b7617d [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;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000050
Chris Lattner0f984262008-03-01 08:50:34 +000051namespace CodeGen {
Chris Lattnerdce14062008-06-26 04:19:03 +000052 class CodeGenModule;
Chris Lattner0f984262008-03-01 08:50:34 +000053
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000054//FIXME Several methods should be pure virtual but aren't to avoid the
55//partially-implemented subclass breaking.
56
57/// Implements runtime-specific code generation functions.
Chris Lattner0f984262008-03-01 08:50:34 +000058class CGObjCRuntime {
Daniel Dunbar58bf6102008-08-11 16:50:21 +000059
Chris Lattner0f984262008-03-01 08:50:34 +000060public:
61 virtual ~CGObjCRuntime();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000062
63 /// Generate the function required to register all Objective-C components in
64 /// this compilation unit with the runtime library.
65 virtual llvm::Function *ModuleInitFunction() = 0;
66
67 /// Get a selector for the specified name and type values. The
68 /// return value should have the LLVM type for pointer-to
69 /// ASTContext::getObjCSelType().
Daniel Dunbar45d196b2008-11-01 01:53:16 +000070 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000071 Selector Sel) = 0;
72
73 /// Generate a constant string object.
74 virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
75
76 /// Generate a category. A category contains a list of methods (and
77 /// accompanying metadata) and a list of protocols.
78 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
79
80 /// Generate a class stucture for this class.
81 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +000082
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000083 /// Generate an Objective-C message send operation.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000084 virtual CodeGen::RValue
85 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000086 QualType ResultType,
87 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000088 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000089 bool IsClassMessage,
90 const CallArgList &CallArgs) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000091
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000092 /// Generate an Objective-C message send operation to the super
Daniel Dunbarf56f1912008-08-25 08:19:24 +000093 /// class initiated in a method for Class and with the given Self
94 /// object.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000095 virtual CodeGen::RValue
96 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000097 QualType ResultType,
98 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000099 const ObjCInterfaceDecl *Class,
100 llvm::Value *Self,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000101 bool IsClassMessage,
102 const CallArgList &CallArgs) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000103
104 /// Emit the code to return the named protocol as an object, as in a
105 /// @protocol expression.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000106 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000107 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000108
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000109 /// Generate the named protocol. Protocols contain method metadata but no
110 /// implementations.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000111 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000112
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000113 /// Generate a function preamble for a method with the specified
114 /// types.
115
116 // FIXME: Current this just generates the Function definition, but
117 // really this should also be generating the loads of the
118 // parameters, as the runtime should have full control over how
119 // parameters are passed.
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000120 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
121 const ObjCContainerDecl *CD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000122
Daniel Dunbar49f66022008-09-24 03:38:44 +0000123 /// Return the runtime function for getting properties.
124 virtual llvm::Function *GetPropertyGetFunction() = 0;
125
126 /// Return the runtime function for setting properties.
127 virtual llvm::Function *GetPropertySetFunction() = 0;
128
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000129 /// GetClass - Return a reference to the class for the given
130 /// interface decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000131 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000132 const ObjCInterfaceDecl *OID) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000133
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000134 /// EnumerationMutationFunction - Return the function that's called by the
135 /// compiler when a mutation is detected during foreach iteration.
136 virtual llvm::Function *EnumerationMutationFunction() = 0;
137
Chris Lattner391d77a2008-03-30 23:03:07 +0000138 /// If instance variable addresses are determined at runtime then this should
139 /// return true, otherwise instance variables will be accessed directly from
140 /// the structure. If this returns true then @defs is invalid for this
141 /// runtime and a warning should be generated.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000142 virtual bool LateBoundIVars() const { return false; }
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000143
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000144 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
145 const Stmt &S) = 0;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000146 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
147 const ObjCAtThrowStmt &S) = 0;
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000148 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Nate Begemanfea86852008-12-16 19:57:09 +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;
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000154 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
155 llvm::Value *src, llvm::Value *dest) = 0;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000156 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
157 llvm::Value *src, llvm::Value *dest) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +0000158};
159
Chris Lattner391d77a2008-03-30 23:03:07 +0000160/// Creates an instance of an Objective-C runtime class.
161//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000162CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
163CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000164CGObjCRuntime *CreateMacModernObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000165}
166}
167#endif