clang API Documentation
00001 //===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This provides an abstract class for Objective-C code generation. Concrete 00011 // subclasses of this implement code generation for specific Objective-C 00012 // runtime libraries. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef CLANG_CODEGEN_OBCJRUNTIME_H 00017 #define CLANG_CODEGEN_OBCJRUNTIME_H 00018 #include "clang/Basic/IdentifierTable.h" // Selector 00019 #include "clang/AST/DeclObjC.h" 00020 00021 #include "CGBuilder.h" 00022 #include "CGCall.h" 00023 #include "CGValue.h" 00024 00025 namespace llvm { 00026 class Constant; 00027 class Function; 00028 class Module; 00029 class StructLayout; 00030 class StructType; 00031 class Type; 00032 class Value; 00033 } 00034 00035 namespace clang { 00036 namespace CodeGen { 00037 class CodeGenFunction; 00038 } 00039 00040 class FieldDecl; 00041 class ObjCAtTryStmt; 00042 class ObjCAtThrowStmt; 00043 class ObjCAtSynchronizedStmt; 00044 class ObjCContainerDecl; 00045 class ObjCCategoryImplDecl; 00046 class ObjCImplementationDecl; 00047 class ObjCInterfaceDecl; 00048 class ObjCMessageExpr; 00049 class ObjCMethodDecl; 00050 class ObjCProtocolDecl; 00051 class Selector; 00052 class ObjCIvarDecl; 00053 class ObjCStringLiteral; 00054 class BlockDeclRefExpr; 00055 00056 namespace CodeGen { 00057 class CodeGenModule; 00058 class CGBlockInfo; 00059 00060 // FIXME: Several methods should be pure virtual but aren't to avoid the 00061 // partially-implemented subclass breaking. 00062 00063 /// Implements runtime-specific code generation functions. 00064 class CGObjCRuntime { 00065 protected: 00066 CodeGen::CodeGenModule &CGM; 00067 CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {} 00068 00069 // Utility functions for unified ivar access. These need to 00070 // eventually be folded into other places (the structure layout 00071 // code). 00072 00073 /// Compute an offset to the given ivar, suitable for passing to 00074 /// EmitValueForIvarAtOffset. Note that the correct handling of 00075 /// bit-fields is carefully coordinated by these two, use caution! 00076 /// 00077 /// The latter overload is suitable for computing the offset of a 00078 /// sythesized ivar. 00079 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 00080 const ObjCInterfaceDecl *OID, 00081 const ObjCIvarDecl *Ivar); 00082 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 00083 const ObjCImplementationDecl *OID, 00084 const ObjCIvarDecl *Ivar); 00085 00086 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, 00087 const ObjCInterfaceDecl *OID, 00088 llvm::Value *BaseValue, 00089 const ObjCIvarDecl *Ivar, 00090 unsigned CVRQualifiers, 00091 llvm::Value *Offset); 00092 /// Emits a try / catch statement. This function is intended to be called by 00093 /// subclasses, and provides a generic mechanism for generating these, which 00094 /// should be usable by all runtimes. The caller must provide the functions to 00095 /// call when entering and exiting a @catch() block, and the function used to 00096 /// rethrow exceptions. If the begin and end catch functions are NULL, then 00097 /// the function assumes that the EH personality function provides the 00098 /// thrown object directly. 00099 void EmitTryCatchStmt(CodeGenFunction &CGF, 00100 const ObjCAtTryStmt &S, 00101 llvm::Constant *beginCatchFn, 00102 llvm::Constant *endCatchFn, 00103 llvm::Constant *exceptionRethrowFn); 00104 /// Emits an @synchronize() statement, using the syncEnterFn and syncExitFn 00105 /// arguments as the functions called to lock and unlock the object. This 00106 /// function can be called by subclasses that use zero-cost exception 00107 /// handling. 00108 void EmitAtSynchronizedStmt(CodeGenFunction &CGF, 00109 const ObjCAtSynchronizedStmt &S, 00110 llvm::Function *syncEnterFn, 00111 llvm::Function *syncExitFn); 00112 00113 public: 00114 virtual ~CGObjCRuntime(); 00115 00116 /// Generate the function required to register all Objective-C components in 00117 /// this compilation unit with the runtime library. 00118 virtual llvm::Function *ModuleInitFunction() = 0; 00119 00120 /// Get a selector for the specified name and type values. The 00121 /// return value should have the LLVM type for pointer-to 00122 /// ASTContext::getObjCSelType(). 00123 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, 00124 Selector Sel, bool lval=false) = 0; 00125 00126 /// Get a typed selector. 00127 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, 00128 const ObjCMethodDecl *Method) = 0; 00129 00130 /// Get the type constant to catch for the given ObjC pointer type. 00131 /// This is used externally to implement catching ObjC types in C++. 00132 /// Runtimes which don't support this should add the appropriate 00133 /// error to Sema. 00134 virtual llvm::Constant *GetEHType(QualType T) = 0; 00135 00136 /// Generate a constant string object. 00137 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0; 00138 00139 /// Generate a category. A category contains a list of methods (and 00140 /// accompanying metadata) and a list of protocols. 00141 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0; 00142 00143 /// Generate a class structure for this class. 00144 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0; 00145 00146 /// Register an class alias. 00147 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0; 00148 00149 /// Generate an Objective-C message send operation. 00150 /// 00151 /// \param Method - The method being called, this may be null if synthesizing 00152 /// a property setter or getter. 00153 virtual CodeGen::RValue 00154 GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 00155 ReturnValueSlot ReturnSlot, 00156 QualType ResultType, 00157 Selector Sel, 00158 llvm::Value *Receiver, 00159 const CallArgList &CallArgs, 00160 const ObjCInterfaceDecl *Class = 0, 00161 const ObjCMethodDecl *Method = 0) = 0; 00162 00163 /// Generate an Objective-C message send operation to the super 00164 /// class initiated in a method for Class and with the given Self 00165 /// object. 00166 /// 00167 /// \param Method - The method being called, this may be null if synthesizing 00168 /// a property setter or getter. 00169 virtual CodeGen::RValue 00170 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 00171 ReturnValueSlot ReturnSlot, 00172 QualType ResultType, 00173 Selector Sel, 00174 const ObjCInterfaceDecl *Class, 00175 bool isCategoryImpl, 00176 llvm::Value *Self, 00177 bool IsClassMessage, 00178 const CallArgList &CallArgs, 00179 const ObjCMethodDecl *Method = 0) = 0; 00180 00181 /// Emit the code to return the named protocol as an object, as in a 00182 /// @protocol expression. 00183 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 00184 const ObjCProtocolDecl *OPD) = 0; 00185 00186 /// Generate the named protocol. Protocols contain method metadata but no 00187 /// implementations. 00188 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0; 00189 00190 /// Generate a function preamble for a method with the specified 00191 /// types. 00192 00193 // FIXME: Current this just generates the Function definition, but really this 00194 // should also be generating the loads of the parameters, as the runtime 00195 // should have full control over how parameters are passed. 00196 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 00197 const ObjCContainerDecl *CD) = 0; 00198 00199 /// Return the runtime function for getting properties. 00200 virtual llvm::Constant *GetPropertyGetFunction() = 0; 00201 00202 /// Return the runtime function for setting properties. 00203 virtual llvm::Constant *GetPropertySetFunction() = 0; 00204 00205 /// Return the runtime function for optimized setting properties. 00206 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, 00207 bool copy) = 0; 00208 00209 // API for atomic copying of qualified aggregates in getter. 00210 virtual llvm::Constant *GetGetStructFunction() = 0; 00211 // API for atomic copying of qualified aggregates in setter. 00212 virtual llvm::Constant *GetSetStructFunction() = 0; 00213 // API for atomic copying of qualified aggregates with non-trivial copy 00214 // assignment (c++) in setter/getter. 00215 virtual llvm::Constant *GetCppAtomicObjectFunction() = 0; 00216 00217 /// GetClass - Return a reference to the class for the given 00218 /// interface decl. 00219 virtual llvm::Value *GetClass(CGBuilderTy &Builder, 00220 const ObjCInterfaceDecl *OID) = 0; 00221 00222 00223 virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { 00224 llvm_unreachable("autoreleasepool unsupported in this ABI"); 00225 } 00226 00227 /// EnumerationMutationFunction - Return the function that's called by the 00228 /// compiler when a mutation is detected during foreach iteration. 00229 virtual llvm::Constant *EnumerationMutationFunction() = 0; 00230 00231 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 00232 const ObjCAtSynchronizedStmt &S) = 0; 00233 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, 00234 const ObjCAtTryStmt &S) = 0; 00235 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 00236 const ObjCAtThrowStmt &S) = 0; 00237 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 00238 llvm::Value *AddrWeakObj) = 0; 00239 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 00240 llvm::Value *src, llvm::Value *dest) = 0; 00241 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 00242 llvm::Value *src, llvm::Value *dest, 00243 bool threadlocal=false) = 0; 00244 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 00245 llvm::Value *src, llvm::Value *dest, 00246 llvm::Value *ivarOffset) = 0; 00247 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 00248 llvm::Value *src, llvm::Value *dest) = 0; 00249 00250 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 00251 QualType ObjectTy, 00252 llvm::Value *BaseValue, 00253 const ObjCIvarDecl *Ivar, 00254 unsigned CVRQualifiers) = 0; 00255 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 00256 const ObjCInterfaceDecl *Interface, 00257 const ObjCIvarDecl *Ivar) = 0; 00258 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, 00259 llvm::Value *DestPtr, 00260 llvm::Value *SrcPtr, 00261 llvm::Value *Size) = 0; 00262 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, 00263 const CodeGen::CGBlockInfo &blockInfo) = 0; 00264 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) = 0; 00265 00266 struct MessageSendInfo { 00267 const CGFunctionInfo &CallInfo; 00268 llvm::PointerType *MessengerType; 00269 00270 MessageSendInfo(const CGFunctionInfo &callInfo, 00271 llvm::PointerType *messengerType) 00272 : CallInfo(callInfo), MessengerType(messengerType) {} 00273 }; 00274 00275 MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method, 00276 QualType resultType, 00277 CallArgList &callArgs); 00278 }; 00279 00280 /// Creates an instance of an Objective-C runtime class. 00281 //TODO: This should include some way of selecting which runtime to target. 00282 CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM); 00283 CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM); 00284 } 00285 } 00286 #endif