blob: d4f7bb07cd097159b96f7c2ff74e7c4e081d37d1 [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
23namespace llvm {
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000024 class Constant;
25 class Type;
26 class Value;
27 class Module;
Chris Lattnerb326b172008-03-30 23:03:07 +000028 class Function;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000029}
30
31namespace clang {
Chris Lattnerd71288e2008-06-26 04:37:12 +000032 class Selector;
33
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000034namespace CodeGen {
Chris Lattner547907c2008-06-26 04:19:03 +000035 class CodeGenModule;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000036
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000037//FIXME Several methods should be pure virtual but aren't to avoid the
38//partially-implemented subclass breaking.
39
40/// Implements runtime-specific code generation functions.
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000041class CGObjCRuntime {
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000042 typedef llvm::IRBuilder<> BuilderType;
43
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000044public:
45 virtual ~CGObjCRuntime();
46
Chris Lattnerb326b172008-03-30 23:03:07 +000047 /// Generate an Objective-C message send operation
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000048 virtual llvm::Value *GenerateMessageSend(BuilderType &Builder,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000049 const llvm::Type *ReturnTy,
Daniel Dunbarfa456242008-08-12 05:08:18 +000050 // FIXME: This should be
51 // dropped, it is unused
52 // and generates a spurious
53 // load.
Chris Lattnerb326b172008-03-30 23:03:07 +000054 llvm::Value *Sender,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000055 llvm::Value *Receiver,
Chris Lattner8384c142008-06-26 04:42:20 +000056 Selector Sel,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000057 llvm::Value** ArgV,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000058 unsigned ArgC) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +000059 /// Generate the function required to register all Objective-C components in
60 /// this compilation unit with the runtime library.
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000061 virtual llvm::Function *ModuleInitFunction() =0;
62 /// Get a selector for the specified name and type values
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000063 virtual llvm::Value *GetSelector(BuilderType &Builder,
Chris Lattnerfaf23db2008-08-08 19:57:58 +000064 Selector Sel) =0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000065 /// Generate a constant string object
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000066 virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000067 /// Generate a category. A category contains a list of methods (and
68 /// accompanying metadata) and a list of protocols.
69 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
Chris Lattner578279d2008-06-26 05:08:00 +000070 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000071 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattner578279d2008-06-26 05:08:00 +000072 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000073 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
74 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
75 /// Generate a class stucture for this class.
76 virtual void GenerateClass(
77 const char *ClassName,
78 const char *SuperClassName,
79 const int instanceSize,
80 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
81 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
82 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
Chris Lattner578279d2008-06-26 05:08:00 +000083 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000084 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattner578279d2008-06-26 05:08:00 +000085 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000086 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
87 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
Chris Lattnerfaf23db2008-08-08 19:57:58 +000088 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder<true> &Builder,
Chris Lattnerd71288e2008-06-26 04:37:12 +000089 const llvm::Type *ReturnTy,
Daniel Dunbarfa456242008-08-12 05:08:18 +000090 // FIXME: This should
91 // be dropped, it is
92 // unused and
93 // generates a
94 // spurious load.
Chris Lattnerd71288e2008-06-26 04:37:12 +000095 llvm::Value *Sender,
96 const char *SuperClassName,
97 llvm::Value *Receiver,
98 Selector Sel,
99 llvm::Value** ArgV,
100 unsigned ArgC) = 0;
Daniel Dunbarfa456242008-08-12 05:08:18 +0000101
102 /// Emit the code to return the named protocol as an object, as in a
103 /// @protocol expression.
104 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
105 const char *ProtocolName) = 0;
106
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000107 /// Generate the named protocol. Protocols contain method metadata but no
108 /// implementations.
109 virtual void GenerateProtocol(const char *ProtocolName,
110 const llvm::SmallVectorImpl<std::string> &Protocols,
111 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
112 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
113 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
114 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +0000115 /// Generate a function preamble for a method with the specified types
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000116 virtual llvm::Function *MethodPreamble(
117 const std::string &ClassName,
118 const std::string &CategoryName,
119 const std::string &MethodName,
120 const llvm::Type *ReturnTy,
Chris Lattnerb326b172008-03-30 23:03:07 +0000121 const llvm::Type *SelfTy,
122 const llvm::Type **ArgTy,
123 unsigned ArgC,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000124 bool isClassMethod,
Chris Lattnerb326b172008-03-30 23:03:07 +0000125 bool isVarArg) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000126 /// Look up the class for the specified name
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +0000127 virtual llvm::Value *LookupClass(BuilderType &Builder,
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000128 llvm::Value *ClassName) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +0000129 /// If instance variable addresses are determined at runtime then this should
130 /// return true, otherwise instance variables will be accessed directly from
131 /// the structure. If this returns true then @defs is invalid for this
132 /// runtime and a warning should be generated.
133 virtual bool LateBoundIVars() { return false; }
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000134};
135
Chris Lattnerb326b172008-03-30 23:03:07 +0000136/// Creates an instance of an Objective-C runtime class.
137//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000138CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
139CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000140}
141}
142#endif