blob: e373ef20a7227ba6d832db30bec388db8bddfed6 [file] [log] [blame]
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
Chris Lattnera0fd5ee2008-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 Dunbarde300732008-08-11 04:54:23 +000018#include "clang/Basic/IdentifierTable.h" // Selector
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000019#include "llvm/ADT/SmallVector.h"
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000020#include "llvm/Support/IRBuilder.h"
Argiris Kirtzidis0560bcf2008-06-01 21:23:24 +000021#include <string>
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000022
Daniel Dunbara04840b2008-08-23 03:46:30 +000023#include "CGValue.h"
24
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000025namespace llvm {
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000026 class Constant;
27 class Type;
28 class Value;
29 class Module;
Chris Lattnerb326b172008-03-30 23:03:07 +000030 class Function;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000031}
32
33namespace clang {
Daniel Dunbara04840b2008-08-23 03:46:30 +000034 namespace CodeGen {
35 class CodeGenFunction;
36 }
37
Daniel Dunbarac93e472008-08-15 22:20:32 +000038 class ObjCCategoryImplDecl;
39 class ObjCImplementationDecl;
Daniel Dunbar434627a2008-08-16 00:25:02 +000040 class ObjCInterfaceDecl;
Daniel Dunbara04840b2008-08-23 03:46:30 +000041 class ObjCMessageExpr;
Daniel Dunbarac93e472008-08-15 22:20:32 +000042 class ObjCMethodDecl;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000043 class ObjCProtocolDecl;
Chris Lattnerd71288e2008-06-26 04:37:12 +000044 class Selector;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000045
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000046namespace CodeGen {
Chris Lattner547907c2008-06-26 04:19:03 +000047 class CodeGenModule;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000048
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000049//FIXME Several methods should be pure virtual but aren't to avoid the
50//partially-implemented subclass breaking.
51
52/// Implements runtime-specific code generation functions.
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000053class CGObjCRuntime {
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000054 typedef llvm::IRBuilder<> BuilderType;
55
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000056public:
57 virtual ~CGObjCRuntime();
Daniel Dunbarac93e472008-08-15 22:20:32 +000058
59 /// Generate the function required to register all Objective-C components in
60 /// this compilation unit with the runtime library.
61 virtual llvm::Function *ModuleInitFunction() = 0;
62
63 /// Get a selector for the specified name and type values. The
64 /// return value should have the LLVM type for pointer-to
65 /// ASTContext::getObjCSelType().
66 virtual llvm::Value *GetSelector(BuilderType &Builder,
67 Selector Sel) = 0;
68
69 /// Generate a constant string object.
70 virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
71
72 /// Generate a category. A category contains a list of methods (and
73 /// accompanying metadata) and a list of protocols.
74 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
75
76 /// Generate a class stucture for this class.
77 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000078
Daniel Dunbarac93e472008-08-15 22:20:32 +000079 /// Generate an Objective-C message send operation.
Daniel Dunbara04840b2008-08-23 03:46:30 +000080 virtual CodeGen::RValue
81 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
82 const ObjCMessageExpr *E,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000083 llvm::Value *Receiver,
84 bool IsClassMessage) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000085
Daniel Dunbarac93e472008-08-15 22:20:32 +000086 /// Generate an Objective-C message send operation to the super
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000087 /// class initiated in a method for Class and with the given Self
88 /// object.
Daniel Dunbara04840b2008-08-23 03:46:30 +000089 virtual CodeGen::RValue
90 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
91 const ObjCMessageExpr *E,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000092 const ObjCInterfaceDecl *Class,
93 llvm::Value *Self,
94 bool IsClassMessage) = 0;
Daniel Dunbarfa456242008-08-12 05:08:18 +000095
96 /// Emit the code to return the named protocol as an object, as in a
97 /// @protocol expression.
98 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
Daniel Dunbarac93e472008-08-15 22:20:32 +000099 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbarfa456242008-08-12 05:08:18 +0000100
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000101 /// Generate the named protocol. Protocols contain method metadata but no
102 /// implementations.
Daniel Dunbarac93e472008-08-15 22:20:32 +0000103 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000104
Daniel Dunbarac93e472008-08-15 22:20:32 +0000105 /// Generate a function preamble for a method with the specified
106 /// types.
107
108 // FIXME: Current this just generates the Function definition, but
109 // really this should also be generating the loads of the
110 // parameters, as the runtime should have full control over how
111 // parameters are passed.
112 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000113
Daniel Dunbar434627a2008-08-16 00:25:02 +0000114 /// GetClass - Return a reference to the class for the given
115 /// interface decl.
116 virtual llvm::Value *GetClass(BuilderType &Builder,
117 const ObjCInterfaceDecl *OID) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000118
Chris Lattnerb326b172008-03-30 23:03:07 +0000119 /// If instance variable addresses are determined at runtime then this should
120 /// return true, otherwise instance variables will be accessed directly from
121 /// the structure. If this returns true then @defs is invalid for this
122 /// runtime and a warning should be generated.
123 virtual bool LateBoundIVars() { return false; }
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000124};
125
Chris Lattnerb326b172008-03-30 23:03:07 +0000126/// Creates an instance of an Objective-C runtime class.
127//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000128CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
129CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000130}
131}
132#endif