CGObjCRuntime.h revision 207619
1193323Sed//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This provides an abstract class for Objective-C code generation. Concrete 11193323Sed// subclasses of this implement code generation for specific Objective-C 12193323Sed// runtime libraries. 13193323Sed// 14193323Sed//===----------------------------------------------------------------------===// 15198090Srdivacky 16193323Sed#ifndef CLANG_CODEGEN_OBCJRUNTIME_H 17193323Sed#define CLANG_CODEGEN_OBCJRUNTIME_H 18198090Srdivacky#include "clang/Basic/IdentifierTable.h" // Selector 19193323Sed#include "clang/AST/DeclObjC.h" 20193323Sed#include <string> 21198090Srdivacky 22198090Srdivacky#include "CGBuilder.h" 23198090Srdivacky#include "CGCall.h" 24198090Srdivacky#include "CGValue.h" 25198090Srdivacky 26198090Srdivackynamespace llvm { 27193323Sed class Constant; 28193323Sed class Function; 29193323Sed class Module; 30193323Sed class StructLayout; 31193323Sed class StructType; 32193323Sed class Type; 33218893Sdim class Value; 34193323Sed} 35193323Sed 36193323Sednamespace clang { 37198090Srdivackynamespace CodeGen { 38198090Srdivacky class CodeGenFunction; 39198090Srdivacky} 40218893Sdim 41199481Srdivacky class FieldDecl; 42218893Sdim class ObjCAtTryStmt; 43218893Sdim class ObjCAtThrowStmt; 44218893Sdim class ObjCAtSynchronizedStmt; 45208599Srdivacky class ObjCContainerDecl; 46193323Sed class ObjCCategoryImplDecl; 47198090Srdivacky class ObjCImplementationDecl; 48198090Srdivacky class ObjCInterfaceDecl; 49198090Srdivacky class ObjCMessageExpr; 50198090Srdivacky class ObjCMethodDecl; 51198090Srdivacky class ObjCProtocolDecl; 52198090Srdivacky class Selector; 53193323Sed class ObjCIvarDecl; 54193323Sed class ObjCStringLiteral; 55193323Sed 56198090Srdivackynamespace CodeGen { 57198090Srdivacky class CodeGenModule; 58198090Srdivacky 59193323Sed// FIXME: Several methods should be pure virtual but aren't to avoid the 60218893Sdim// partially-implemented subclass breaking. 61193323Sed 62193323Sed/// Implements runtime-specific code generation functions. 63218893Sdimclass CGObjCRuntime { 64193323Sedprotected: 65193323Sed // Utility functions for unified ivar access. These need to 66193323Sed // eventually be folded into other places (the structure layout 67193323Sed // code). 68193323Sed 69218893Sdim /// Compute an offset to the given ivar, suitable for passing to 70218893Sdim /// EmitValueForIvarAtOffset. Note that the correct handling of 71193323Sed /// bit-fields is carefully coordinated by these two, use caution! 72193323Sed /// 73218893Sdim /// The latter overload is suitable for computing the offset of a 74193323Sed /// sythesized ivar. 75193323Sed uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 76193323Sed const ObjCInterfaceDecl *OID, 77193323Sed const ObjCIvarDecl *Ivar); 78221345Sdim uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 79221345Sdim const ObjCImplementationDecl *OID, 80221345Sdim const ObjCIvarDecl *Ivar); 81221345Sdim 82221345Sdim LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, 83221345Sdim const ObjCInterfaceDecl *OID, 84 llvm::Value *BaseValue, 85 const ObjCIvarDecl *Ivar, 86 unsigned CVRQualifiers, 87 llvm::Value *Offset); 88 89public: 90 virtual ~CGObjCRuntime(); 91 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; 95 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) = 0; 101 102 /// Get a typed selector. 103 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, 104 const ObjCMethodDecl *Method) = 0; 105 106 /// Generate a constant string object. 107 virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0; 108 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; 112 113 /// Generate a class stucture for this class. 114 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0; 115 116 /// Generate an Objective-C message send operation. 117 /// 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 QualType ResultType, 123 Selector Sel, 124 llvm::Value *Receiver, 125 const CallArgList &CallArgs, 126 const ObjCInterfaceDecl *Class = 0, 127 const ObjCMethodDecl *Method = 0) = 0; 128 129 /// Generate an Objective-C message send operation to the super 130 /// class initiated in a method for Class and with the given Self 131 /// object. 132 /// 133 /// \param Method - The method being called, this may be null if synthesizing 134 /// a property setter or getter. 135 virtual CodeGen::RValue 136 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 137 QualType ResultType, 138 Selector Sel, 139 const ObjCInterfaceDecl *Class, 140 bool isCategoryImpl, 141 llvm::Value *Self, 142 bool IsClassMessage, 143 const CallArgList &CallArgs, 144 const ObjCMethodDecl *Method = 0) = 0; 145 146 /// Emit the code to return the named protocol as an object, as in a 147 /// @protocol expression. 148 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 149 const ObjCProtocolDecl *OPD) = 0; 150 151 /// Generate the named protocol. Protocols contain method metadata but no 152 /// implementations. 153 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0; 154 155 /// Generate a function preamble for a method with the specified 156 /// types. 157 158 // FIXME: Current this just generates the Function definition, but really this 159 // should also be generating the loads of the parameters, as the runtime 160 // should have full control over how parameters are passed. 161 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 162 const ObjCContainerDecl *CD) = 0; 163 164 /// Return the runtime function for getting properties. 165 virtual llvm::Constant *GetPropertyGetFunction() = 0; 166 167 /// Return the runtime function for setting properties. 168 virtual llvm::Constant *GetPropertySetFunction() = 0; 169 170 // API for atomic copying of qualified aggregates in setter/getter. 171 virtual llvm::Constant *GetCopyStructFunction() = 0; 172 173 /// GetClass - Return a reference to the class for the given 174 /// interface decl. 175 virtual llvm::Value *GetClass(CGBuilderTy &Builder, 176 const ObjCInterfaceDecl *OID) = 0; 177 178 /// EnumerationMutationFunction - Return the function that's called by the 179 /// compiler when a mutation is detected during foreach iteration. 180 virtual llvm::Constant *EnumerationMutationFunction() = 0; 181 182 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 183 const Stmt &S) = 0; 184 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 185 const ObjCAtThrowStmt &S) = 0; 186 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 187 llvm::Value *AddrWeakObj) = 0; 188 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 189 llvm::Value *src, llvm::Value *dest) = 0; 190 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 191 llvm::Value *src, llvm::Value *dest) = 0; 192 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 193 llvm::Value *src, llvm::Value *dest, 194 llvm::Value *ivarOffset) = 0; 195 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 196 llvm::Value *src, llvm::Value *dest) = 0; 197 198 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 199 QualType ObjectTy, 200 llvm::Value *BaseValue, 201 const ObjCIvarDecl *Ivar, 202 unsigned CVRQualifiers) = 0; 203 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 204 const ObjCInterfaceDecl *Interface, 205 const ObjCIvarDecl *Ivar) = 0; 206 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, 207 llvm::Value *DestPtr, 208 llvm::Value *SrcPtr, 209 QualType Ty) = 0; 210}; 211 212/// Creates an instance of an Objective-C runtime class. 213//TODO: This should include some way of selecting which runtime to target. 214CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM); 215CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM); 216CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM); 217} 218} 219#endif 220