blob: a354aa19c2c8c1ab64ad235c35f2bfc96986165f [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 {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000032 class ObjCProtocolDecl;
Chris Lattnerd71288e2008-06-26 04:37:12 +000033 class Selector;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000034
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000035namespace CodeGen {
Chris Lattner547907c2008-06-26 04:19:03 +000036 class CodeGenModule;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000037
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000038//FIXME Several methods should be pure virtual but aren't to avoid the
39//partially-implemented subclass breaking.
40
41/// Implements runtime-specific code generation functions.
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000042class CGObjCRuntime {
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000043 typedef llvm::IRBuilder<> BuilderType;
44
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000045public:
46 virtual ~CGObjCRuntime();
47
Chris Lattnerb326b172008-03-30 23:03:07 +000048 /// Generate an Objective-C message send operation
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000049 virtual llvm::Value *GenerateMessageSend(BuilderType &Builder,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000050 const llvm::Type *ReturnTy,
51 llvm::Value *Receiver,
Chris Lattner8384c142008-06-26 04:42:20 +000052 Selector Sel,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000053 llvm::Value** ArgV,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000054 unsigned ArgC) =0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000055
Chris Lattnerb326b172008-03-30 23:03:07 +000056 /// Generate the function required to register all Objective-C components in
57 /// this compilation unit with the runtime library.
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000058 virtual llvm::Function *ModuleInitFunction() =0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000059
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000060 /// Get a selector for the specified name and type values
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000061 virtual llvm::Value *GetSelector(BuilderType &Builder,
Chris Lattnerfaf23db2008-08-08 19:57:58 +000062 Selector Sel) =0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000063
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000064 /// Generate a constant string object
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000065 virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000066
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;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000075
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000076 /// Generate a class stucture for this class.
77 virtual void GenerateClass(
78 const char *ClassName,
79 const char *SuperClassName,
80 const int instanceSize,
81 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
82 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
83 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
Chris Lattner578279d2008-06-26 05:08:00 +000084 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000085 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattner578279d2008-06-26 05:08:00 +000086 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000087 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
88 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000089
Chris Lattnerfaf23db2008-08-08 19:57:58 +000090 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder<true> &Builder,
Chris Lattnerd71288e2008-06-26 04:37:12 +000091 const llvm::Type *ReturnTy,
Chris Lattnerd71288e2008-06-26 04:37:12 +000092 const char *SuperClassName,
93 llvm::Value *Receiver,
94 Selector Sel,
95 llvm::Value** ArgV,
96 unsigned ArgC) = 0;
Daniel Dunbarfa456242008-08-12 05:08:18 +000097
98 /// Emit the code to return the named protocol as an object, as in a
99 /// @protocol expression.
100 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000101 const ObjCProtocolDecl *PD) = 0;
Daniel Dunbarfa456242008-08-12 05:08:18 +0000102
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000103 /// Generate the named protocol. Protocols contain method metadata but no
104 /// implementations.
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000105 virtual void GenerateProtocol(const ObjCProtocolDecl *PD) = 0;
106
Chris Lattnerb326b172008-03-30 23:03:07 +0000107 /// Generate a function preamble for a method with the specified types
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000108 virtual llvm::Function *MethodPreamble(
109 const std::string &ClassName,
110 const std::string &CategoryName,
111 const std::string &MethodName,
112 const llvm::Type *ReturnTy,
Chris Lattnerb326b172008-03-30 23:03:07 +0000113 const llvm::Type *SelfTy,
114 const llvm::Type **ArgTy,
115 unsigned ArgC,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000116 bool isClassMethod,
Chris Lattnerb326b172008-03-30 23:03:07 +0000117 bool isVarArg) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000118
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000119 /// Look up the class for the specified name
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +0000120 virtual llvm::Value *LookupClass(BuilderType &Builder,
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000121 llvm::Value *ClassName) =0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000122
Chris Lattnerb326b172008-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.
127 virtual bool LateBoundIVars() { return false; }
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000128};
129
Chris Lattnerb326b172008-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 Dunbar8c85fac2008-08-11 02:45:11 +0000132CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
133CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000134}
135}
136#endif