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