blob: 54591a331c6a6328e3d56fb26a7a9be1c1aabbe5 [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"
Daniel Dunbar58bf6102008-08-11 16:50:21 +000020#include "llvm/Support/IRBuilder.h"
Argyrios Kyrtzidis8ef07c02008-06-01 21:23:24 +000021#include <string>
Chris Lattner0f984262008-03-01 08:50:34 +000022
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000023#include "CGValue.h"
24
Chris Lattner0f984262008-03-01 08:50:34 +000025namespace llvm {
Chris Lattner0f984262008-03-01 08:50:34 +000026 class Constant;
27 class Type;
28 class Value;
29 class Module;
Chris Lattner391d77a2008-03-30 23:03:07 +000030 class Function;
Chris Lattner0f984262008-03-01 08:50:34 +000031}
32
33namespace clang {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000034 namespace CodeGen {
35 class CodeGenFunction;
36 }
37
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000038 class ObjCCategoryImplDecl;
39 class ObjCImplementationDecl;
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000040 class ObjCInterfaceDecl;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000041 class ObjCMessageExpr;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000042 class ObjCMethodDecl;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000043 class ObjCProtocolDecl;
Chris Lattner8e67b632008-06-26 04:37:12 +000044 class Selector;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000045
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000046 typedef llvm::SmallVector<std::pair<llvm::Value*, QualType>, 16> CallArgList;
47
Chris Lattner0f984262008-03-01 08:50:34 +000048namespace CodeGen {
Chris Lattnerdce14062008-06-26 04:19:03 +000049 class CodeGenModule;
Chris Lattner0f984262008-03-01 08:50:34 +000050
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000051//FIXME Several methods should be pure virtual but aren't to avoid the
52//partially-implemented subclass breaking.
53
54/// Implements runtime-specific code generation functions.
Chris Lattner0f984262008-03-01 08:50:34 +000055class CGObjCRuntime {
Daniel Dunbar58bf6102008-08-11 16:50:21 +000056 typedef llvm::IRBuilder<> BuilderType;
57
Chris Lattner0f984262008-03-01 08:50:34 +000058public:
59 virtual ~CGObjCRuntime();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000060
61 /// Generate the function required to register all Objective-C components in
62 /// this compilation unit with the runtime library.
63 virtual llvm::Function *ModuleInitFunction() = 0;
64
65 /// Get a selector for the specified name and type values. The
66 /// return value should have the LLVM type for pointer-to
67 /// ASTContext::getObjCSelType().
68 virtual llvm::Value *GetSelector(BuilderType &Builder,
69 Selector Sel) = 0;
70
71 /// Generate a constant string object.
72 virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
73
74 /// Generate a category. A category contains a list of methods (and
75 /// accompanying metadata) and a list of protocols.
76 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
77
78 /// Generate a class stucture for this class.
79 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +000080
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000081 /// Generate an Objective-C message send operation.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000082 virtual CodeGen::RValue
83 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
84 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000085 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000086 bool IsClassMessage,
87 const CallArgList &CallArgs) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000088
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000089 /// Generate an Objective-C message send operation to the super
Daniel Dunbarf56f1912008-08-25 08:19:24 +000090 /// class initiated in a method for Class and with the given Self
91 /// object.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000092 virtual CodeGen::RValue
93 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
94 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000095 const ObjCInterfaceDecl *Class,
96 llvm::Value *Self,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000097 bool IsClassMessage,
98 const CallArgList &CallArgs) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +000099
100 /// Emit the code to return the named protocol as an object, as in a
101 /// @protocol expression.
102 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000103 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar98c5ead2008-08-12 05:08:18 +0000104
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000105 /// Generate the named protocol. Protocols contain method metadata but no
106 /// implementations.
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000107 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000108
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000109 /// Generate a function preamble for a method with the specified
110 /// types.
111
112 // FIXME: Current this just generates the Function definition, but
113 // really this should also be generating the loads of the
114 // parameters, as the runtime should have full control over how
115 // parameters are passed.
116 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000117
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000118 /// GetClass - Return a reference to the class for the given
119 /// interface decl.
120 virtual llvm::Value *GetClass(BuilderType &Builder,
121 const ObjCInterfaceDecl *OID) = 0;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000122
Chris Lattner391d77a2008-03-30 23:03:07 +0000123 /// If instance variable addresses are determined at runtime then this should
124 /// return true, otherwise instance variables will be accessed directly from
125 /// the structure. If this returns true then @defs is invalid for this
126 /// runtime and a warning should be generated.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000127 virtual bool LateBoundIVars() const { return false; }
Chris Lattner0f984262008-03-01 08:50:34 +0000128};
129
Chris Lattner391d77a2008-03-30 23:03:07 +0000130/// Creates an instance of an Objective-C runtime class.
131//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000132CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
133CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000134}
135}
136#endif