blob: 07d89620965dd3bcb9b02108bc1a200dc5a1d631 [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 {
Chris Lattnerd71288e2008-06-26 04:37:12 +000031 class Selector;
32
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000033namespace CodeGen {
Chris Lattner547907c2008-06-26 04:19:03 +000034 class CodeGenModule;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000035
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000036//FIXME Several methods should be pure virtual but aren't to avoid the
37//partially-implemented subclass breaking.
38
39/// Implements runtime-specific code generation functions.
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000040class CGObjCRuntime {
41public:
42 virtual ~CGObjCRuntime();
43
Chris Lattnerb326b172008-03-30 23:03:07 +000044 /// Generate an Objective-C message send operation
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000045 virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder &Builder,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000046 const llvm::Type *ReturnTy,
Chris Lattnerb326b172008-03-30 23:03:07 +000047 llvm::Value *Sender,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000048 llvm::Value *Receiver,
Chris Lattnerb326b172008-03-30 23:03:07 +000049 llvm::Value *Selector,
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000050 llvm::Value** ArgV,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000051 unsigned ArgC) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +000052 /// Generate the function required to register all Objective-C components in
53 /// this compilation unit with the runtime library.
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000054 virtual llvm::Function *ModuleInitFunction() =0;
55 /// Get a selector for the specified name and type values
Chris Lattner469cb3e2008-06-26 04:38:58 +000056 virtual llvm::Value *GetSelector(llvm::IRBuilder &Builder, Selector Sel) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000057 virtual llvm::Value *GetSelector(llvm::IRBuilder &Builder,
Chris Lattner769c55f2008-06-26 04:24:57 +000058 llvm::Value *SelName,
59 llvm::Value *SelTypes) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000060 /// Generate a constant string object
Chris Lattner469cb3e2008-06-26 04:38:58 +000061 virtual llvm::Constant *GenerateConstantString(const char *String,
62 const size_t Length) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000063 /// Generate a category. A category contains a list of methods (and
64 /// accompanying metadata) and a list of protocols.
65 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
66 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
67 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
68 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
69 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
70 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
71 /// Generate a class stucture for this class.
72 virtual void GenerateClass(
73 const char *ClassName,
74 const char *SuperClassName,
75 const int instanceSize,
76 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
77 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
78 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
79 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
80 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
81 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
82 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
83 const llvm::SmallVectorImpl<std::string> &Protocols) =0;
84 /// Generate a reference to the named protocol.
85 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder &Builder, const char
86 *ProtocolName) =0;
87 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder &Builder,
Chris Lattnerd71288e2008-06-26 04:37:12 +000088 const llvm::Type *ReturnTy,
89 llvm::Value *Sender,
90 const char *SuperClassName,
91 llvm::Value *Receiver,
92 Selector Sel,
93 llvm::Value** ArgV,
94 unsigned ArgC) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000095 /// Generate the named protocol. Protocols contain method metadata but no
96 /// implementations.
97 virtual void GenerateProtocol(const char *ProtocolName,
98 const llvm::SmallVectorImpl<std::string> &Protocols,
99 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
100 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
101 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
102 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +0000103 /// Generate a function preamble for a method with the specified types
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000104 virtual llvm::Function *MethodPreamble(
105 const std::string &ClassName,
106 const std::string &CategoryName,
107 const std::string &MethodName,
108 const llvm::Type *ReturnTy,
Chris Lattnerb326b172008-03-30 23:03:07 +0000109 const llvm::Type *SelfTy,
110 const llvm::Type **ArgTy,
111 unsigned ArgC,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000112 bool isClassMethod,
Chris Lattnerb326b172008-03-30 23:03:07 +0000113 bool isVarArg) = 0;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000114 /// Look up the class for the specified name
115 virtual llvm::Value *LookupClass(llvm::IRBuilder &Builder, llvm::Value
116 *ClassName) =0;
Chris Lattnerb326b172008-03-30 23:03:07 +0000117 /// If instance variable addresses are determined at runtime then this should
118 /// return true, otherwise instance variables will be accessed directly from
119 /// the structure. If this returns true then @defs is invalid for this
120 /// runtime and a warning should be generated.
121 virtual bool LateBoundIVars() { return false; }
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000122};
123
Chris Lattnerb326b172008-03-30 23:03:07 +0000124/// Creates an instance of an Objective-C runtime class.
125//TODO: This should include some way of selecting which runtime to target.
Chris Lattner547907c2008-06-26 04:19:03 +0000126CGObjCRuntime *CreateObjCRuntime(CodeGenModule &CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000127}
128}
129#endif