blob: 4610ef8b65e0ff1b3ad2f9330589f19c0c1f47df [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
23namespace llvm {
Chris Lattner0f984262008-03-01 08:50:34 +000024 class Constant;
25 class Type;
26 class Value;
27 class Module;
Chris Lattner391d77a2008-03-30 23:03:07 +000028 class Function;
Chris Lattner0f984262008-03-01 08:50:34 +000029}
30
31namespace clang {
Chris Lattner8e67b632008-06-26 04:37:12 +000032 class Selector;
33
Chris Lattner0f984262008-03-01 08:50:34 +000034namespace CodeGen {
Chris Lattnerdce14062008-06-26 04:19:03 +000035 class CodeGenModule;
Chris Lattner0f984262008-03-01 08:50:34 +000036
Anton Korobeynikov20ff3102008-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 Lattner0f984262008-03-01 08:50:34 +000041class CGObjCRuntime {
Daniel Dunbar58bf6102008-08-11 16:50:21 +000042 typedef llvm::IRBuilder<> BuilderType;
43
Chris Lattner0f984262008-03-01 08:50:34 +000044public:
45 virtual ~CGObjCRuntime();
46
Chris Lattner391d77a2008-03-30 23:03:07 +000047 /// Generate an Objective-C message send operation
Daniel Dunbar58bf6102008-08-11 16:50:21 +000048 virtual llvm::Value *GenerateMessageSend(BuilderType &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +000049 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +000050 llvm::Value *Sender,
Chris Lattner0f984262008-03-01 08:50:34 +000051 llvm::Value *Receiver,
Chris Lattner9384c762008-06-26 04:42:20 +000052 Selector Sel,
Chris Lattner0f984262008-03-01 08:50:34 +000053 llvm::Value** ArgV,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000054 unsigned ArgC) =0;
Chris Lattner391d77a2008-03-30 23:03:07 +000055 /// Generate the function required to register all Objective-C components in
56 /// this compilation unit with the runtime library.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000057 virtual llvm::Function *ModuleInitFunction() =0;
58 /// Get a selector for the specified name and type values
Daniel Dunbar58bf6102008-08-11 16:50:21 +000059 virtual llvm::Value *GetSelector(BuilderType &Builder,
Chris Lattner85e35682008-08-08 19:57:58 +000060 Selector Sel) =0;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000061 /// Generate a constant string object
Chris Lattner42ba3e72008-06-26 04:38:58 +000062 virtual llvm::Constant *GenerateConstantString(const char *String,
63 const size_t Length) = 0;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000064 /// Generate a category. A category contains a list of methods (and
65 /// accompanying metadata) and a list of protocols.
66 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000067 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000068 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +000069 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000070 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
71 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
72 /// Generate a class stucture for this class.
73 virtual void GenerateClass(
74 const char *ClassName,
75 const char *SuperClassName,
76 const int instanceSize,
77 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
78 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
79 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
Chris Lattnera4210072008-06-26 05:08:00 +000080 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000081 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +000082 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000083 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
84 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
85 /// Generate a reference to the named protocol.
Chris Lattner85e35682008-08-08 19:57:58 +000086 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000087 const char *ProtocolName) = 0;
Chris Lattner85e35682008-08-08 19:57:58 +000088 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder<true> &Builder,
Chris Lattner8e67b632008-06-26 04:37:12 +000089 const llvm::Type *ReturnTy,
90 llvm::Value *Sender,
91 const char *SuperClassName,
92 llvm::Value *Receiver,
93 Selector Sel,
94 llvm::Value** ArgV,
95 unsigned ArgC) = 0;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000096 /// Generate the named protocol. Protocols contain method metadata but no
97 /// implementations.
98 virtual void GenerateProtocol(const char *ProtocolName,
99 const llvm::SmallVectorImpl<std::string> &Protocols,
100 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
101 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
102 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
103 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) =0;
Chris Lattner391d77a2008-03-30 23:03:07 +0000104 /// Generate a function preamble for a method with the specified types
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000105 virtual llvm::Function *MethodPreamble(
106 const std::string &ClassName,
107 const std::string &CategoryName,
108 const std::string &MethodName,
109 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000110 const llvm::Type *SelfTy,
111 const llvm::Type **ArgTy,
112 unsigned ArgC,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000113 bool isClassMethod,
Chris Lattner391d77a2008-03-30 23:03:07 +0000114 bool isVarArg) = 0;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000115 /// Look up the class for the specified name
Daniel Dunbar58bf6102008-08-11 16:50:21 +0000116 virtual llvm::Value *LookupClass(BuilderType &Builder,
Chris Lattner85e35682008-08-08 19:57:58 +0000117 llvm::Value *ClassName) =0;
Chris Lattner391d77a2008-03-30 23:03:07 +0000118 /// If instance variable addresses are determined at runtime then this should
119 /// return true, otherwise instance variables will be accessed directly from
120 /// the structure. If this returns true then @defs is invalid for this
121 /// runtime and a warning should be generated.
122 virtual bool LateBoundIVars() { return false; }
Chris Lattner0f984262008-03-01 08:50:34 +0000123};
124
Chris Lattner391d77a2008-03-30 23:03:07 +0000125/// Creates an instance of an Objective-C runtime class.
126//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000127CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
128CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000129}
130}
131#endif