CGObjCRuntime.h revision 2bebbf0acee55404de4b8846713b64429e744e8f
1//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This provides an abstract class for Objective-C code generation. Concrete 11// subclasses of this implement code generation for specific Objective-C 12// runtime libraries. 13// 14//===----------------------------------------------------------------------===// 15 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 <string> 21 22#include "CGBuilder.h" 23#include "CGCall.h" 24#include "CGValue.h" 25 26namespace llvm { 27 class Constant; 28 class Function; 29 class Module; 30 class StructLayout; 31 class StructType; 32 class Type; 33 class Value; 34} 35 36namespace clang { 37namespace CodeGen { 38 class CodeGenFunction; 39} 40 41 class FieldDecl; 42 class ObjCAtTryStmt; 43 class ObjCAtThrowStmt; 44 class ObjCAtSynchronizedStmt; 45 class ObjCContainerDecl; 46 class ObjCCategoryImplDecl; 47 class ObjCImplementationDecl; 48 class ObjCInterfaceDecl; 49 class ObjCMessageExpr; 50 class ObjCMethodDecl; 51 class ObjCProtocolDecl; 52 class Selector; 53 class ObjCIvarDecl; 54 class ObjCStringLiteral; 55 56namespace CodeGen { 57 class CodeGenModule; 58 59//FIXME Several methods should be pure virtual but aren't to avoid the 60//partially-implemented subclass breaking. 61 62/// Implements runtime-specific code generation functions. 63class CGObjCRuntime { 64public: 65 // Utility functions for unified ivar access. These need to 66 // eventually be folded into other places (the structure layout 67 // code). 68 69protected: 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! 73 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, 74 const ObjCInterfaceDecl *OID, 75 const ObjCIvarDecl *Ivar); 76 77 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, 78 const ObjCInterfaceDecl *OID, 79 llvm::Value *BaseValue, 80 const ObjCIvarDecl *Ivar, 81 unsigned CVRQualifiers, 82 llvm::Value *Offset); 83 84public: 85 virtual ~CGObjCRuntime(); 86 87 /// Generate the function required to register all Objective-C components in 88 /// this compilation unit with the runtime library. 89 virtual llvm::Function *ModuleInitFunction() = 0; 90 91 /// Get a selector for the specified name and type values. The 92 /// return value should have the LLVM type for pointer-to 93 /// ASTContext::getObjCSelType(). 94 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, 95 Selector Sel) = 0; 96 97 /// Generate a constant string object. 98 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0; 99 100 /// Generate a category. A category contains a list of methods (and 101 /// accompanying metadata) and a list of protocols. 102 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0; 103 104 /// Generate a class stucture for this class. 105 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0; 106 107 /// Generate an Objective-C message send operation. 108 virtual CodeGen::RValue 109 GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 110 QualType ResultType, 111 Selector Sel, 112 llvm::Value *Receiver, 113 bool IsClassMessage, 114 const CallArgList &CallArgs) = 0; 115 116 /// Generate an Objective-C message send operation to the super 117 /// class initiated in a method for Class and with the given Self 118 /// object. 119 virtual CodeGen::RValue 120 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 121 QualType ResultType, 122 Selector Sel, 123 const ObjCInterfaceDecl *Class, 124 bool isCategoryImpl, 125 llvm::Value *Self, 126 bool IsClassMessage, 127 const CallArgList &CallArgs) = 0; 128 129 /// Emit the code to return the named protocol as an object, as in a 130 /// @protocol expression. 131 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 132 const ObjCProtocolDecl *OPD) = 0; 133 134 /// Generate the named protocol. Protocols contain method metadata but no 135 /// implementations. 136 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0; 137 138 /// Generate a function preamble for a method with the specified 139 /// types. 140 141 // FIXME: Current this just generates the Function definition, but 142 // really this should also be generating the loads of the 143 // parameters, as the runtime should have full control over how 144 // parameters are passed. 145 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 146 const ObjCContainerDecl *CD) = 0; 147 148 /// Return the runtime function for getting properties. 149 virtual llvm::Constant *GetPropertyGetFunction() = 0; 150 151 /// Return the runtime function for setting properties. 152 virtual llvm::Constant *GetPropertySetFunction() = 0; 153 154 /// GetClass - Return a reference to the class for the given 155 /// interface decl. 156 virtual llvm::Value *GetClass(CGBuilderTy &Builder, 157 const ObjCInterfaceDecl *OID) = 0; 158 159 /// EnumerationMutationFunction - Return the function that's called by the 160 /// compiler when a mutation is detected during foreach iteration. 161 virtual llvm::Constant *EnumerationMutationFunction() = 0; 162 163 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 164 const Stmt &S) = 0; 165 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 166 const ObjCAtThrowStmt &S) = 0; 167 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 168 llvm::Value *AddrWeakObj) = 0; 169 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 170 llvm::Value *src, llvm::Value *dest) = 0; 171 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 172 llvm::Value *src, llvm::Value *dest) = 0; 173 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 174 llvm::Value *src, llvm::Value *dest) = 0; 175 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 176 llvm::Value *src, llvm::Value *dest) = 0; 177 178 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 179 QualType ObjectTy, 180 llvm::Value *BaseValue, 181 const ObjCIvarDecl *Ivar, 182 unsigned CVRQualifiers) = 0; 183 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 184 const ObjCInterfaceDecl *Interface, 185 const ObjCIvarDecl *Ivar) = 0; 186}; 187 188/// Creates an instance of an Objective-C runtime class. 189//TODO: This should include some way of selecting which runtime to target. 190CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM); 191CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM); 192CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM); 193} 194} 195#endif 196