CGObjCRuntime.h revision 8f2926b73ed635afecd020da787af6a837601a2b
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
46namespace CodeGen {
47  class CodeGenModule;
48
49//FIXME Several methods should be pure virtual but aren't to avoid the
50//partially-implemented subclass breaking.
51
52/// Implements runtime-specific code generation functions.
53class CGObjCRuntime {
54  typedef llvm::IRBuilder<> BuilderType;
55
56public:
57  virtual ~CGObjCRuntime();
58
59  /// Generate the function required to register all Objective-C components in
60  /// this compilation unit with the runtime library.
61  virtual llvm::Function *ModuleInitFunction() = 0;
62
63  /// Get a selector for the specified name and type values. The
64  /// return value should have the LLVM type for pointer-to
65  /// ASTContext::getObjCSelType().
66  virtual llvm::Value *GetSelector(BuilderType &Builder,
67                                   Selector Sel) = 0;
68
69  /// Generate a constant string object.
70  virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
71
72  /// Generate a category.  A category contains a list of methods (and
73  /// accompanying metadata) and a list of protocols.
74  virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
75
76  /// Generate a class stucture for this class.
77  virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
78
79  /// Generate an Objective-C message send operation.
80  virtual CodeGen::RValue
81  GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
82                      const ObjCMessageExpr *E,
83                      llvm::Value *Receiver) = 0;
84
85  /// Generate an Objective-C message send operation to the super
86  /// class.
87  virtual CodeGen::RValue
88  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
89                           const ObjCMessageExpr *E,
90                           const ObjCInterfaceDecl *SuperClass,
91                           llvm::Value *Receiver) = 0;
92
93  /// Emit the code to return the named protocol as an object, as in a
94  /// @protocol expression.
95  virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
96                                           const ObjCProtocolDecl *OPD) = 0;
97
98  /// Generate the named protocol.  Protocols contain method metadata but no
99  /// implementations.
100  virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
101
102  /// Generate a function preamble for a method with the specified
103  /// types.
104
105  // FIXME: Current this just generates the Function definition, but
106  // really this should also be generating the loads of the
107  // parameters, as the runtime should have full control over how
108  // parameters are passed.
109  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD) = 0;
110
111  /// GetClass - Return a reference to the class for the given
112  /// interface decl.
113  virtual llvm::Value *GetClass(BuilderType &Builder,
114                                const ObjCInterfaceDecl *OID) = 0;
115
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