1 //===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides an abstract class for Objective-C code generation. Concrete
11 // subclasses of this implement code generation for specific Objective-C
14 //===----------------------------------------------------------------------===//
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 "clang/AST/DeclObjC.h"
23 #include "CGBuilder.h"
39 class CodeGenFunction;
44 class ObjCAtThrowStmt;
45 class ObjCAtSynchronizedStmt;
46 class ObjCContainerDecl;
47 class ObjCCategoryImplDecl;
48 class ObjCImplementationDecl;
49 class ObjCInterfaceDecl;
50 class ObjCMessageExpr;
52 class ObjCProtocolDecl;
55 class ObjCStringLiteral;
60 // FIXME: Several methods should be pure virtual but aren't to avoid the
61 // partially-implemented subclass breaking.
63 /// Implements runtime-specific code generation functions.
66 // Utility functions for unified ivar access. These need to
67 // eventually be folded into other places (the structure layout
71 /// Compute an offset to the given ivar, suitable for passing to
72 /// EmitValueForIvarAtOffset. Note that the correct handling of
73 /// bit-fields is carefully coordinated by these two, use caution!
75 /// The latter overload is suitable for computing the offset of a
77 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
78 const ObjCInterfaceDecl *OID,
79 const ObjCIvarDecl *Ivar);
80 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
81 const ObjCImplementationDecl *OID,
82 const ObjCIvarDecl *Ivar);
84 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
85 const ObjCInterfaceDecl *OID,
86 llvm::Value *BaseValue,
87 const ObjCIvarDecl *Ivar,
88 unsigned CVRQualifiers,
92 virtual ~CGObjCRuntime();
94 /// Generate the function required to register all Objective-C components in
95 /// this compilation unit with the runtime library.
96 virtual llvm::Function *ModuleInitFunction() = 0;
98 /// Get a selector for the specified name and type values. The
99 /// return value should have the LLVM type for pointer-to
100 /// ASTContext::getObjCSelType().
101 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
104 /// Get a typed selector.
105 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
106 const ObjCMethodDecl *Method) = 0;
108 /// Generate a constant string object.
109 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0;
111 /// Generate a category. A category contains a list of methods (and
112 /// accompanying metadata) and a list of protocols.
113 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
115 /// Generate a class stucture for this class.
116 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
118 /// Generate an Objective-C message send operation.
120 /// \param Method - The method being called, this may be null if synthesizing
121 /// a property setter or getter.
122 virtual CodeGen::RValue
123 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
126 llvm::Value *Receiver,
128 const CallArgList &CallArgs,
129 const ObjCMethodDecl *Method = 0) = 0;
131 /// Generate an Objective-C message send operation to the super
132 /// class initiated in a method for Class and with the given Self
135 /// \param Method - The method being called, this may be null if synthesizing
136 /// a property setter or getter.
137 virtual CodeGen::RValue
138 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
141 const ObjCInterfaceDecl *Class,
145 const CallArgList &CallArgs,
146 const ObjCMethodDecl *Method = 0) = 0;
148 /// Emit the code to return the named protocol as an object, as in a
149 /// @protocol expression.
150 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
151 const ObjCProtocolDecl *OPD) = 0;
153 /// Generate the named protocol. Protocols contain method metadata but no
155 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
157 /// Generate a function preamble for a method with the specified
160 // FIXME: Current this just generates the Function definition, but really this
161 // should also be generating the loads of the parameters, as the runtime
162 // should have full control over how parameters are passed.
163 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
164 const ObjCContainerDecl *CD) = 0;
166 /// Return the runtime function for getting properties.
167 virtual llvm::Constant *GetPropertyGetFunction() = 0;
169 /// Return the runtime function for setting properties.
170 virtual llvm::Constant *GetPropertySetFunction() = 0;
172 /// GetClass - Return a reference to the class for the given
174 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
175 const ObjCInterfaceDecl *OID) = 0;
177 /// EnumerationMutationFunction - Return the function that's called by the
178 /// compiler when a mutation is detected during foreach iteration.
179 virtual llvm::Constant *EnumerationMutationFunction() = 0;
181 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
183 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
184 const ObjCAtThrowStmt &S) = 0;
185 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
186 llvm::Value *AddrWeakObj) = 0;
187 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
188 llvm::Value *src, llvm::Value *dest) = 0;
189 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
190 llvm::Value *src, llvm::Value *dest) = 0;
191 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
192 llvm::Value *src, llvm::Value *dest,
193 llvm::Value *ivarOffset) = 0;
194 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
195 llvm::Value *src, llvm::Value *dest) = 0;
197 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
199 llvm::Value *BaseValue,
200 const ObjCIvarDecl *Ivar,
201 unsigned CVRQualifiers) = 0;
202 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
203 const ObjCInterfaceDecl *Interface,
204 const ObjCIvarDecl *Ivar) = 0;
205 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
206 llvm::Value *DestPtr,
211 /// Creates an instance of an Objective-C runtime class.
212 //TODO: This should include some way of selecting which runtime to target.
213 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
214 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
215 CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);