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"
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
70 /// Compute an offset to the given ivar, suitable for passing to
71 /// EmitValueForIvarAtOffset. Note that the correct handling of
72 /// bit-fields is carefully coordinated by these two, use caution!
74 /// The latter overload is suitable for computing the offset of a
76 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
77 const ObjCInterfaceDecl *OID,
78 const ObjCIvarDecl *Ivar);
79 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
80 const ObjCImplementationDecl *OID,
81 const ObjCIvarDecl *Ivar);
83 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
84 const ObjCInterfaceDecl *OID,
85 llvm::Value *BaseValue,
86 const ObjCIvarDecl *Ivar,
87 unsigned CVRQualifiers,
91 virtual ~CGObjCRuntime();
93 /// Generate the function required to register all Objective-C components in
94 /// this compilation unit with the runtime library.
95 virtual llvm::Function *ModuleInitFunction() = 0;
97 /// Get a selector for the specified name and type values. The
98 /// return value should have the LLVM type for pointer-to
99 /// ASTContext::getObjCSelType().
100 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
103 /// Generate a constant string object.
104 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0;
106 /// Generate a category. A category contains a list of methods (and
107 /// accompanying metadata) and a list of protocols.
108 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
110 /// Generate a class stucture for this class.
111 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
113 /// Generate an Objective-C message send operation.
114 virtual CodeGen::RValue
115 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
118 llvm::Value *Receiver,
120 const CallArgList &CallArgs) = 0;
122 /// Generate an Objective-C message send operation to the super
123 /// class initiated in a method for Class and with the given Self
125 virtual CodeGen::RValue
126 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
129 const ObjCInterfaceDecl *Class,
133 const CallArgList &CallArgs) = 0;
135 /// Emit the code to return the named protocol as an object, as in a
136 /// @protocol expression.
137 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
138 const ObjCProtocolDecl *OPD) = 0;
140 /// Generate the named protocol. Protocols contain method metadata but no
142 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
144 /// Generate a function preamble for a method with the specified
147 // FIXME: Current this just generates the Function definition, but
148 // really this should also be generating the loads of the
149 // parameters, as the runtime should have full control over how
150 // parameters are passed.
151 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
152 const ObjCContainerDecl *CD) = 0;
154 /// Return the runtime function for getting properties.
155 virtual llvm::Constant *GetPropertyGetFunction() = 0;
157 /// Return the runtime function for setting properties.
158 virtual llvm::Constant *GetPropertySetFunction() = 0;
160 /// GetClass - Return a reference to the class for the given
162 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
163 const ObjCInterfaceDecl *OID) = 0;
165 /// EnumerationMutationFunction - Return the function that's called by the
166 /// compiler when a mutation is detected during foreach iteration.
167 virtual llvm::Constant *EnumerationMutationFunction() = 0;
169 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
171 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
172 const ObjCAtThrowStmt &S) = 0;
173 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
174 llvm::Value *AddrWeakObj) = 0;
175 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
176 llvm::Value *src, llvm::Value *dest) = 0;
177 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
178 llvm::Value *src, llvm::Value *dest) = 0;
179 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
180 llvm::Value *src, llvm::Value *dest) = 0;
181 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
182 llvm::Value *src, llvm::Value *dest) = 0;
184 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
186 llvm::Value *BaseValue,
187 const ObjCIvarDecl *Ivar,
188 unsigned CVRQualifiers) = 0;
189 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
190 const ObjCInterfaceDecl *Interface,
191 const ObjCIvarDecl *Ivar) = 0;
194 /// Creates an instance of an Objective-C runtime class.
195 //TODO: This should include some way of selecting which runtime to target.
196 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
197 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
198 CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM);