blob: c03857fab0fd872bafdd46f358e6e29ead92e1c2 [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"
Argiris Kirtzidis0560bcf2008-06-01 21:23:24 +000020#include <string>
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000021
22namespace llvm {
Chris Lattnerfaf23db2008-08-08 19:57:58 +000023 template<bool C> class IRBuilder;
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 {
42public:
43 virtual ~CGObjCRuntime();
44
Chris Lattnerb326b172008-03-30 23:03:07 +000045 /// Generate an Objective-C message send operation
Chris Lattnerfaf23db2008-08-08 19:57:58 +000046 virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder<true> &Builder,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000047 const llvm::Type *ReturnTy,
Chris Lattnerb326b172008-03-30 23:03:07 +000048 llvm::Value *Sender,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000049 llvm::Value *Receiver,
Chris Lattner8384c142008-06-26 04:42:20 +000050 Selector Sel,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000051 llvm::Value** ArgV,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000052 unsigned ArgC) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +000053 /// Generate the function required to register all Objective-C components in
54 /// this compilation unit with the runtime library.
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000055 virtual llvm::Function *ModuleInitFunction() =0;
56 /// Get a selector for the specified name and type values
Chris Lattnerfaf23db2008-08-08 19:57:58 +000057 virtual llvm::Value *GetSelector(llvm::IRBuilder<true> &Builder,
58 Selector Sel) =0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000059 /// Generate a constant string object
Chris Lattner469cb3e2008-06-26 04:38:58 +000060 virtual llvm::Constant *GenerateConstantString(const char *String,
61 const size_t Length) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000062 /// Generate a category. A category contains a list of methods (and
63 /// accompanying metadata) and a list of protocols.
64 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
Chris Lattner578279d2008-06-26 05:08:00 +000065 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000066 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattner578279d2008-06-26 05:08:00 +000067 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000068 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
69 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
70 /// Generate a class stucture for this class.
71 virtual void GenerateClass(
72 const char *ClassName,
73 const char *SuperClassName,
74 const int instanceSize,
75 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
76 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
77 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
Chris Lattner578279d2008-06-26 05:08:00 +000078 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000079 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattner578279d2008-06-26 05:08:00 +000080 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000081 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
82 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
83 /// Generate a reference to the named protocol.
Chris Lattnerfaf23db2008-08-08 19:57:58 +000084 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000085 const char *ProtocolName) = 0;
Chris Lattnerfaf23db2008-08-08 19:57:58 +000086 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder<true> &Builder,
Chris Lattnerd71288e2008-06-26 04:37:12 +000087 const llvm::Type *ReturnTy,
88 llvm::Value *Sender,
89 const char *SuperClassName,
90 llvm::Value *Receiver,
91 Selector Sel,
92 llvm::Value** ArgV,
93 unsigned ArgC) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000094 /// Generate the named protocol. Protocols contain method metadata but no
95 /// implementations.
96 virtual void GenerateProtocol(const char *ProtocolName,
97 const llvm::SmallVectorImpl<std::string> &Protocols,
98 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
99 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
100 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
101 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +0000102 /// Generate a function preamble for a method with the specified types
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000103 virtual llvm::Function *MethodPreamble(
104 const std::string &ClassName,
105 const std::string &CategoryName,
106 const std::string &MethodName,
107 const llvm::Type *ReturnTy,
Chris Lattnerb326b172008-03-30 23:03:07 +0000108 const llvm::Type *SelfTy,
109 const llvm::Type **ArgTy,
110 unsigned ArgC,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000111 bool isClassMethod,
Chris Lattnerb326b172008-03-30 23:03:07 +0000112 bool isVarArg) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000113 /// Look up the class for the specified name
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000114 virtual llvm::Value *LookupClass(llvm::IRBuilder<true> &Builder,
115 llvm::Value *ClassName) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +0000116 /// If instance variable addresses are determined at runtime then this should
117 /// return true, otherwise instance variables will be accessed directly from
118 /// the structure. If this returns true then @defs is invalid for this
119 /// runtime and a warning should be generated.
120 virtual bool LateBoundIVars() { return false; }
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000121};
122
Chris Lattnerb326b172008-03-30 23:03:07 +0000123/// Creates an instance of an Objective-C runtime class.
124//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000125CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
126CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000127}
128}
129#endif