CGObjCRuntime.h revision 19cd87eb5fb3c197e631ce08fd52c446c4d4e8f1
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 "llvm/Support/IRBuilder.h"
21#include <string>
22
23#include "CGValue.h"
24
25namespace llvm {
26  class Constant;
27  class Type;
28  class Value;
29  class Module;
30  class Function;
31}
32
33namespace clang {
34  namespace CodeGen {
35    class CodeGenFunction;
36  }
37
38  class ObjCCategoryImplDecl;
39  class ObjCImplementationDecl;
40  class ObjCInterfaceDecl;
41  class ObjCMessageExpr;
42  class ObjCMethodDecl;
43  class ObjCProtocolDecl;
44  class Selector;
45
46  typedef llvm::SmallVector<std::pair<llvm::Value*, QualType>, 16> CallArgList;
47
48namespace CodeGen {
49  class CodeGenModule;
50
51//FIXME Several methods should be pure virtual but aren't to avoid the
52//partially-implemented subclass breaking.
53
54/// Implements runtime-specific code generation functions.
55class CGObjCRuntime {
56  typedef llvm::IRBuilder<> BuilderType;
57
58public:
59  virtual ~CGObjCRuntime();
60
61  /// Generate the function required to register all Objective-C components in
62  /// this compilation unit with the runtime library.
63  virtual llvm::Function *ModuleInitFunction() = 0;
64
65  /// Get a selector for the specified name and type values. The
66  /// return value should have the LLVM type for pointer-to
67  /// ASTContext::getObjCSelType().
68  virtual llvm::Value *GetSelector(BuilderType &Builder,
69                                   Selector Sel) = 0;
70
71  /// Generate a constant string object.
72  virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
73
74  /// Generate a category.  A category contains a list of methods (and
75  /// accompanying metadata) and a list of protocols.
76  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
77
78  /// Generate a class stucture for this class.
79  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
80
81  /// Generate an Objective-C message send operation.
82  virtual CodeGen::RValue
83  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
84                      const ObjCMessageExpr *E,
85                      llvm::Value *Receiver,
86                      bool IsClassMessage,
87                      const CallArgList &CallArgs) = 0;
88
89  /// Generate an Objective-C message send operation to the super
90  /// class initiated in a method for Class and with the given Self
91  /// object.
92  virtual CodeGen::RValue
93  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
94                           const ObjCMessageExpr *E,
95                           const ObjCInterfaceDecl *Class,
96                           llvm::Value *Self,
97                           bool IsClassMessage,
98                           const CallArgList &CallArgs) = 0;
99
100  /// Emit the code to return the named protocol as an object, as in a
101  /// @protocol expression.
102  virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
103                                           const ObjCProtocolDecl *OPD) = 0;
104
105  /// Generate the named protocol.  Protocols contain method metadata but no
106  /// implementations.
107  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
108
109  /// Generate a function preamble for a method with the specified
110  /// types.
111
112  // FIXME: Current this just generates the Function definition, but
113  // really this should also be generating the loads of the
114  // parameters, as the runtime should have full control over how
115  // parameters are passed.
116  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD) = 0;
117
118  /// GetClass - Return a reference to the class for the given
119  /// interface decl.
120  virtual llvm::Value *GetClass(BuilderType &Builder,
121                                const ObjCInterfaceDecl *OID) = 0;
122
123  /// If instance variable addresses are determined at runtime then this should
124  /// return true, otherwise instance variables will be accessed directly from
125  /// the structure.  If this returns true then @defs is invalid for this
126  /// runtime and a warning should be generated.
127  virtual bool LateBoundIVars() const { return false; }
128};
129
130/// Creates an instance of an Objective-C runtime class.
131//TODO: This should include some way of selecting which runtime to target.
132CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
133CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
134}
135}
136#endif
137