CGObjCRuntime.h revision e91593ef084479340582b2ba177b44be50a717b7
1//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
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
18#include "clang/Basic/IdentifierTable.h" // Selector
19#include "llvm/ADT/SmallVector.h"
20#include <string>
21
22namespace llvm {
23  template<bool C> class IRBuilder;
24  class Constant;
25  class Type;
26  class Value;
27  class Module;
28  class Function;
29}
30
31namespace clang {
32  class Selector;
33
34namespace CodeGen {
35  class CodeGenModule;
36
37//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.
41class CGObjCRuntime {
42public:
43  virtual ~CGObjCRuntime();
44
45  /// Generate an Objective-C message send operation
46  virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder<true> &Builder,
47                                           const llvm::Type *ReturnTy,
48                                           llvm::Value *Sender,
49                                           llvm::Value *Receiver,
50                                           Selector Sel,
51                                           llvm::Value** ArgV,
52                                           unsigned ArgC) =0;
53  /// Generate the function required to register all Objective-C components in
54  /// this compilation unit with the runtime library.
55  virtual llvm::Function *ModuleInitFunction() =0;
56  /// Get a selector for the specified name and type values
57  virtual llvm::Value *GetSelector(llvm::IRBuilder<true> &Builder,
58                                   Selector Sel) =0;
59  /// Generate a constant string object
60  virtual llvm::Constant *GenerateConstantString(const char *String,
61                                                 const size_t Length) = 0;
62  /// Generate a category.  A category contains a list of methods (and
63  /// accompanying metadata) and a list of protocols.
64  virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
65             const llvm::SmallVectorImpl<Selector>  &InstanceMethodSels,
66             const llvm::SmallVectorImpl<llvm::Constant *>  &InstanceMethodTypes,
67             const llvm::SmallVectorImpl<Selector>  &ClassMethodSels,
68             const llvm::SmallVectorImpl<llvm::Constant *>  &ClassMethodTypes,
69             const llvm::SmallVectorImpl<std::string> &Protocols) =0;
70  /// Generate a class stucture for this class.
71  virtual void GenerateClass(
72             const char *ClassName,
73             const char *SuperClassName,
74             const int instanceSize,
75             const llvm::SmallVectorImpl<llvm::Constant *>  &IvarNames,
76             const llvm::SmallVectorImpl<llvm::Constant *>  &IvarTypes,
77             const llvm::SmallVectorImpl<llvm::Constant *>  &IvarOffsets,
78             const llvm::SmallVectorImpl<Selector>  &InstanceMethodSels,
79             const llvm::SmallVectorImpl<llvm::Constant *>  &InstanceMethodTypes,
80             const llvm::SmallVectorImpl<Selector>  &ClassMethodSels,
81             const llvm::SmallVectorImpl<llvm::Constant *>  &ClassMethodTypes,
82             const llvm::SmallVectorImpl<std::string> &Protocols) =0;
83  /// Generate a reference to the named protocol.
84  virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
85                                           const char *ProtocolName) = 0;
86  virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder<true> &Builder,
87                                                const llvm::Type *ReturnTy,
88                                                llvm::Value *Sender,
89                                                const char *SuperClassName,
90                                                llvm::Value *Receiver,
91                                                Selector Sel,
92                                                llvm::Value** ArgV,
93                                                unsigned ArgC) = 0;
94  /// Generate the named protocol.  Protocols contain method metadata but no
95  /// implementations.
96  virtual void GenerateProtocol(const char *ProtocolName,
97    const llvm::SmallVectorImpl<std::string> &Protocols,
98    const llvm::SmallVectorImpl<llvm::Constant *>  &InstanceMethodNames,
99    const llvm::SmallVectorImpl<llvm::Constant *>  &InstanceMethodTypes,
100    const llvm::SmallVectorImpl<llvm::Constant *>  &ClassMethodNames,
101    const llvm::SmallVectorImpl<llvm::Constant *>  &ClassMethodTypes) =0;
102  /// Generate a function preamble for a method with the specified types
103  virtual llvm::Function *MethodPreamble(
104                                         const std::string &ClassName,
105                                         const std::string &CategoryName,
106                                         const std::string &MethodName,
107                                         const llvm::Type *ReturnTy,
108                                         const llvm::Type *SelfTy,
109                                         const llvm::Type **ArgTy,
110                                         unsigned ArgC,
111                                         bool isClassMethod,
112                                         bool isVarArg) = 0;
113  /// Look up the class for the specified name
114  virtual llvm::Value *LookupClass(llvm::IRBuilder<true> &Builder,
115                                   llvm::Value *ClassName) =0;
116  /// If instance variable addresses are determined at runtime then this should
117  /// return true, otherwise instance variables will be accessed directly from
118  /// the structure.  If this returns true then @defs is invalid for this
119  /// runtime and a warning should be generated.
120  virtual bool LateBoundIVars() { return false; }
121};
122
123/// Creates an instance of an Objective-C runtime class.
124//TODO: This should include some way of selecting which runtime to target.
125CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
126CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
127}
128}
129#endif
130