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