blob: 7b00725f23cd60c6186d79401d196cf81564c908 [file] [log] [blame]
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
Chris Lattnera0fd5ee2008-03-01 08:50:34 +00002//
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
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/Basic/IdentifierTable.h" // Selector
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000019#include "llvm/ADT/SmallVector.h"
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000020#include "llvm/Support/IRBuilder.h"
Argiris Kirtzidis0560bcf2008-06-01 21:23:24 +000021#include <string>
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000022
Daniel Dunbara04840b2008-08-23 03:46:30 +000023#include "CGValue.h"
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000024#include "CGCall.h"
Daniel Dunbara04840b2008-08-23 03:46:30 +000025
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000026namespace llvm {
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000027 class Constant;
28 class Type;
29 class Value;
30 class Module;
Chris Lattnerb326b172008-03-30 23:03:07 +000031 class Function;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000032}
33
34namespace clang {
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000035namespace CodeGen {
36 class CodeGenFunction;
37}
Daniel Dunbara04840b2008-08-23 03:46:30 +000038
Daniel Dunbarac93e472008-08-15 22:20:32 +000039 class ObjCCategoryImplDecl;
40 class ObjCImplementationDecl;
Daniel Dunbar434627a2008-08-16 00:25:02 +000041 class ObjCInterfaceDecl;
Daniel Dunbara04840b2008-08-23 03:46:30 +000042 class ObjCMessageExpr;
Daniel Dunbarac93e472008-08-15 22:20:32 +000043 class ObjCMethodDecl;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000044 class ObjCProtocolDecl;
Chris Lattnerd71288e2008-06-26 04:37:12 +000045 class Selector;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000046
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000047namespace CodeGen {
Chris Lattner547907c2008-06-26 04:19:03 +000048 class CodeGenModule;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000049
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000050//FIXME Several methods should be pure virtual but aren't to avoid the
51//partially-implemented subclass breaking.
52
53/// Implements runtime-specific code generation functions.
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000054class CGObjCRuntime {
Daniel Dunbarfd2ff6c2008-08-11 16:50:21 +000055 typedef llvm::IRBuilder<> BuilderType;
56
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000057public:
58 virtual ~CGObjCRuntime();
Daniel Dunbarac93e472008-08-15 22:20:32 +000059
60 /// Generate the function required to register all Objective-C components in
61 /// this compilation unit with the runtime library.
62 virtual llvm::Function *ModuleInitFunction() = 0;
63
64 /// Get a selector for the specified name and type values. The
65 /// return value should have the LLVM type for pointer-to
66 /// ASTContext::getObjCSelType().
67 virtual llvm::Value *GetSelector(BuilderType &Builder,
68 Selector Sel) = 0;
69
70 /// Generate a constant string object.
71 virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
72
73 /// Generate a category. A category contains a list of methods (and
74 /// accompanying metadata) and a list of protocols.
75 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
76
77 /// Generate a class stucture for this class.
78 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000079
Daniel Dunbarac93e472008-08-15 22:20:32 +000080 /// Generate an Objective-C message send operation.
Daniel Dunbara04840b2008-08-23 03:46:30 +000081 virtual CodeGen::RValue
82 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +000083 QualType ResultType,
84 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000085 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000086 bool IsClassMessage,
87 const CallArgList &CallArgs) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000088
Daniel Dunbarac93e472008-08-15 22:20:32 +000089 /// Generate an Objective-C message send operation to the super
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000090 /// class initiated in a method for Class and with the given Self
91 /// object.
Daniel Dunbara04840b2008-08-23 03:46:30 +000092 virtual CodeGen::RValue
93 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +000094 QualType ResultType,
95 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000096 const ObjCInterfaceDecl *Class,
97 llvm::Value *Self,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000098 bool IsClassMessage,
99 const CallArgList &CallArgs) = 0;
Daniel Dunbarfa456242008-08-12 05:08:18 +0000100
101 /// Emit the code to return the named protocol as an object, as in a
102 /// @protocol expression.
103 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<true> &Builder,
Daniel Dunbarac93e472008-08-15 22:20:32 +0000104 const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbarfa456242008-08-12 05:08:18 +0000105
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000106 /// Generate the named protocol. Protocols contain method metadata but no
107 /// implementations.
Daniel Dunbarac93e472008-08-15 22:20:32 +0000108 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000109
Daniel Dunbarac93e472008-08-15 22:20:32 +0000110 /// Generate a function preamble for a method with the specified
111 /// types.
112
113 // FIXME: Current this just generates the Function definition, but
114 // really this should also be generating the loads of the
115 // parameters, as the runtime should have full control over how
116 // parameters are passed.
117 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000118
Daniel Dunbar434627a2008-08-16 00:25:02 +0000119 /// GetClass - Return a reference to the class for the given
120 /// interface decl.
121 virtual llvm::Value *GetClass(BuilderType &Builder,
122 const ObjCInterfaceDecl *OID) = 0;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000123
Anders Carlsson58d16242008-08-31 04:05:03 +0000124 /// EnumerationMutationFunction - Return the function that's called by the
125 /// compiler when a mutation is detected during foreach iteration.
126 virtual llvm::Function *EnumerationMutationFunction() = 0;
127
Chris Lattnerb326b172008-03-30 23:03:07 +0000128 /// If instance variable addresses are determined at runtime then this should
129 /// return true, otherwise instance variables will be accessed directly from
130 /// the structure. If this returns true then @defs is invalid for this
131 /// runtime and a warning should be generated.
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000132 virtual bool LateBoundIVars() const { return false; }
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000133};
134
Chris Lattnerb326b172008-03-30 23:03:07 +0000135/// Creates an instance of an Objective-C runtime class.
136//TODO: This should include some way of selecting which runtime to target.
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000137CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
138CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000139}
140}
141#endif