blob: cc33982e475e64bcb767f424cea1d9947d0a7c3e [file] [log] [blame]
Chris Lattner0f984262008-03-01 08:50:34 +00001//===----- CGObjCRuntime.h - Emit LLVM Code from ASTs for a Module --------===//
2//
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
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000018#include "llvm/ADT/SmallVector.h"
Argyrios Kyrtzidis8ef07c02008-06-01 21:23:24 +000019#include <string>
Chris Lattner0f984262008-03-01 08:50:34 +000020
21namespace llvm {
Chris Lattner50b36742008-04-13 07:32:11 +000022 class IRBuilder;
Chris Lattner0f984262008-03-01 08:50:34 +000023 class Constant;
24 class Type;
25 class Value;
26 class Module;
Chris Lattner391d77a2008-03-30 23:03:07 +000027 class Function;
Chris Lattner0f984262008-03-01 08:50:34 +000028}
29
30namespace clang {
31namespace CodeGen {
Chris Lattnerdce14062008-06-26 04:19:03 +000032 class CodeGenModule;
Chris Lattner0f984262008-03-01 08:50:34 +000033
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000034//FIXME Several methods should be pure virtual but aren't to avoid the
35//partially-implemented subclass breaking.
36
37/// Implements runtime-specific code generation functions.
Chris Lattner0f984262008-03-01 08:50:34 +000038class CGObjCRuntime {
39public:
40 virtual ~CGObjCRuntime();
41
Chris Lattner391d77a2008-03-30 23:03:07 +000042 /// Generate an Objective-C message send operation
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000043 virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +000044 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +000045 llvm::Value *Sender,
Chris Lattner0f984262008-03-01 08:50:34 +000046 llvm::Value *Receiver,
Chris Lattner391d77a2008-03-30 23:03:07 +000047 llvm::Value *Selector,
Chris Lattner0f984262008-03-01 08:50:34 +000048 llvm::Value** ArgV,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000049 unsigned ArgC) =0;
Chris Lattner391d77a2008-03-30 23:03:07 +000050 /// Generate the function required to register all Objective-C components in
51 /// this compilation unit with the runtime library.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000052 virtual llvm::Function *ModuleInitFunction() =0;
53 /// Get a selector for the specified name and type values
54 virtual llvm::Value *GetSelector(llvm::IRBuilder &Builder,
Chris Lattner3aba07c2008-06-26 04:24:57 +000055 llvm::Value *SelName,
56 llvm::Value *SelTypes) = 0;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000057 /// Generate a constant string object
58 virtual llvm::Constant *GenerateConstantString(const char *String, const size_t
59 length) =0;
60 /// Generate a category. A category contains a list of methods (and
61 /// accompanying metadata) and a list of protocols.
62 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
63 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
64 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
65 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
66 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
67 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
68 /// Generate a class stucture for this class.
69 virtual void GenerateClass(
70 const char *ClassName,
71 const char *SuperClassName,
72 const int instanceSize,
73 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
74 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
75 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
76 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
77 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
78 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
79 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
80 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
81 /// Generate a reference to the named protocol.
82 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder &Builder, const char
83 *ProtocolName) =0;
84 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder &Builder,
85 const llvm::Type *ReturnTy,
86 llvm::Value *Sender,
87 const char *SuperClassName,
88 llvm::Value *Receiver,
89 llvm::Value *Selector,
90 llvm::Value** ArgV,
91 unsigned ArgC) {return NULL;};
92 /// Generate the named protocol. Protocols contain method metadata but no
93 /// implementations.
94 virtual void GenerateProtocol(const char *ProtocolName,
95 const llvm::SmallVectorImpl<std::string> &Protocols,
96 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
97 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
98 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
99 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) =0;
Chris Lattner391d77a2008-03-30 23:03:07 +0000100 /// Generate a function preamble for a method with the specified types
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000101 virtual llvm::Function *MethodPreamble(
102 const std::string &ClassName,
103 const std::string &CategoryName,
104 const std::string &MethodName,
105 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000106 const llvm::Type *SelfTy,
107 const llvm::Type **ArgTy,
108 unsigned ArgC,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000109 bool isClassMethod,
Chris Lattner391d77a2008-03-30 23:03:07 +0000110 bool isVarArg) = 0;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000111 /// Look up the class for the specified name
112 virtual llvm::Value *LookupClass(llvm::IRBuilder &Builder, llvm::Value
113 *ClassName) =0;
Chris Lattner391d77a2008-03-30 23:03:07 +0000114 /// If instance variable addresses are determined at runtime then this should
115 /// return true, otherwise instance variables will be accessed directly from
116 /// the structure. If this returns true then @defs is invalid for this
117 /// runtime and a warning should be generated.
118 virtual bool LateBoundIVars() { return false; }
Chris Lattner0f984262008-03-01 08:50:34 +0000119};
120
Chris Lattner391d77a2008-03-30 23:03:07 +0000121/// Creates an instance of an Objective-C runtime class.
122//TODO: This should include some way of selecting which runtime to target.
Chris Lattnerdce14062008-06-26 04:19:03 +0000123CGObjCRuntime *CreateObjCRuntime(CodeGenModule &CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000124}
125}
126#endif