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 "clang/AST/DeclObjC.h"
22 #include "CGBuilder.h"
38 class CodeGenFunction;
43 class ObjCAtThrowStmt;
44 class ObjCAtSynchronizedStmt;
45 class ObjCContainerDecl;
46 class ObjCCategoryImplDecl;
47 class ObjCImplementationDecl;
48 class ObjCInterfaceDecl;
49 class ObjCMessageExpr;
51 class ObjCProtocolDecl;
54 class ObjCStringLiteral;
59 // FIXME: Several methods should be pure virtual but aren't to avoid the
60 // partially-implemented subclass breaking.
62 /// Implements runtime-specific code generation functions.
65 // Utility functions for unified ivar access. These need to
66 // eventually be folded into other places (the structure layout
69 /// Compute an offset to the given ivar, suitable for passing to
70 /// EmitValueForIvarAtOffset. Note that the correct handling of
71 /// bit-fields is carefully coordinated by these two, use caution!
73 /// The latter overload is suitable for computing the offset of a
75 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
76 const ObjCInterfaceDecl *OID,
77 const ObjCIvarDecl *Ivar);
78 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
79 const ObjCImplementationDecl *OID,
80 const ObjCIvarDecl *Ivar);
82 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
83 const ObjCInterfaceDecl *OID,
84 llvm::Value *BaseValue,
85 const ObjCIvarDecl *Ivar,
86 unsigned CVRQualifiers,
90 virtual ~CGObjCRuntime();
92 /// Generate the function required to register all Objective-C components in
93 /// this compilation unit with the runtime library.
94 virtual llvm::Function *ModuleInitFunction() = 0;
96 /// Get a selector for the specified name and type values. The
97 /// return value should have the LLVM type for pointer-to
98 /// ASTContext::getObjCSelType().
99 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
100 Selector Sel, bool lval=false) = 0;
102 /// Get a typed selector.
103 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
104 const ObjCMethodDecl *Method) = 0;
106 /// Generate a constant string object.
107 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
109 /// Generate a category. A category contains a list of methods (and
110 /// accompanying metadata) and a list of protocols.
111 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
113 /// Generate a class stucture for this class.
114 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
116 /// Generate an Objective-C message send operation.
118 /// \param Method - The method being called, this may be null if synthesizing
119 /// a property setter or getter.
120 virtual CodeGen::RValue
121 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
122 ReturnValueSlot ReturnSlot,
125 llvm::Value *Receiver,
126 const CallArgList &CallArgs,
127 const ObjCInterfaceDecl *Class = 0,
128 const ObjCMethodDecl *Method = 0) = 0;
130 /// Generate an Objective-C message send operation to the super
131 /// class initiated in a method for Class and with the given Self
134 /// \param Method - The method being called, this may be null if synthesizing
135 /// a property setter or getter.
136 virtual CodeGen::RValue
137 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
138 ReturnValueSlot ReturnSlot,
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 // API for atomic copying of qualified aggregates in setter/getter.
173 virtual llvm::Constant *GetCopyStructFunction() = 0;
175 /// GetClass - Return a reference to the class for the given
177 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
178 const ObjCInterfaceDecl *OID) = 0;
180 /// EnumerationMutationFunction - Return the function that's called by the
181 /// compiler when a mutation is detected during foreach iteration.
182 virtual llvm::Constant *EnumerationMutationFunction() = 0;
184 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
185 const ObjCAtSynchronizedStmt &S) = 0;
186 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
187 const ObjCAtTryStmt &S) = 0;
188 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
189 const ObjCAtThrowStmt &S) = 0;
190 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
191 llvm::Value *AddrWeakObj) = 0;
192 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
193 llvm::Value *src, llvm::Value *dest) = 0;
194 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
195 llvm::Value *src, llvm::Value *dest) = 0;
196 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
197 llvm::Value *src, llvm::Value *dest,
198 llvm::Value *ivarOffset) = 0;
199 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
200 llvm::Value *src, llvm::Value *dest) = 0;
202 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
204 llvm::Value *BaseValue,
205 const ObjCIvarDecl *Ivar,
206 unsigned CVRQualifiers) = 0;
207 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
208 const ObjCInterfaceDecl *Interface,
209 const ObjCIvarDecl *Ivar) = 0;
210 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
211 llvm::Value *DestPtr,
213 llvm::Value *Size) = 0;
216 /// Creates an instance of an Objective-C runtime class.
217 //TODO: This should include some way of selecting which runtime to target.
218 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
219 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
220 CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);