CGObjCRuntime.h revision 33fdb738a6c125f4c788733897021b7c1a062b0c
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 Type; 29 class Value; 30 class Module; 31 class Function; 32} 33 34namespace clang { 35namespace CodeGen { 36 class CodeGenFunction; 37} 38 39 class ObjCAtTryStmt; 40 class ObjCAtThrowStmt; 41 class ObjCAtSynchronizedStmt; 42 class ObjCContainerDecl; 43 class ObjCCategoryImplDecl; 44 class ObjCImplementationDecl; 45 class ObjCInterfaceDecl; 46 class ObjCMessageExpr; 47 class ObjCMethodDecl; 48 class ObjCProtocolDecl; 49 class Selector; 50 class ObjCIvarDecl; 51 class ObjCStringLiteral; 52 53namespace CodeGen { 54 class CodeGenModule; 55 56//FIXME Several methods should be pure virtual but aren't to avoid the 57//partially-implemented subclass breaking. 58 59/// Implements runtime-specific code generation functions. 60class CGObjCRuntime { 61 62public: 63 virtual ~CGObjCRuntime(); 64 65 /// Generate the function required to register all Objective-C components in 66 /// this compilation unit with the runtime library. 67 virtual llvm::Function *ModuleInitFunction() = 0; 68 69 /// Get a selector for the specified name and type values. The 70 /// return value should have the LLVM type for pointer-to 71 /// ASTContext::getObjCSelType(). 72 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, 73 Selector Sel) = 0; 74 75 /// Generate a constant string object. 76 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0; 77 78 /// Generate a category. A category contains a list of methods (and 79 /// accompanying metadata) and a list of protocols. 80 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0; 81 82 /// Generate a class stucture for this class. 83 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0; 84 85 /// Generate an Objective-C message send operation. 86 virtual CodeGen::RValue 87 GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 88 QualType ResultType, 89 Selector Sel, 90 llvm::Value *Receiver, 91 bool IsClassMessage, 92 const CallArgList &CallArgs) = 0; 93 94 /// Generate an Objective-C message send operation to the super 95 /// class initiated in a method for Class and with the given Self 96 /// object. 97 virtual CodeGen::RValue 98 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 99 QualType ResultType, 100 Selector Sel, 101 const ObjCInterfaceDecl *Class, 102 bool isCategoryImpl, 103 llvm::Value *Self, 104 bool IsClassMessage, 105 const CallArgList &CallArgs) = 0; 106 107 /// Emit the code to return the named protocol as an object, as in a 108 /// @protocol expression. 109 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 110 const ObjCProtocolDecl *OPD) = 0; 111 112 /// Generate the named protocol. Protocols contain method metadata but no 113 /// implementations. 114 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0; 115 116 /// Generate a function preamble for a method with the specified 117 /// types. 118 119 // FIXME: Current this just generates the Function definition, but 120 // really this should also be generating the loads of the 121 // parameters, as the runtime should have full control over how 122 // parameters are passed. 123 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 124 const ObjCContainerDecl *CD) = 0; 125 126 /// Return the runtime function for getting properties. 127 virtual llvm::Constant *GetPropertyGetFunction() = 0; 128 129 /// Return the runtime function for setting properties. 130 virtual llvm::Constant *GetPropertySetFunction() = 0; 131 132 /// GetClass - Return a reference to the class for the given 133 /// interface decl. 134 virtual llvm::Value *GetClass(CGBuilderTy &Builder, 135 const ObjCInterfaceDecl *OID) = 0; 136 137 /// EnumerationMutationFunction - Return the function that's called by the 138 /// compiler when a mutation is detected during foreach iteration. 139 virtual llvm::Constant *EnumerationMutationFunction() = 0; 140 141 /// If instance variable addresses are determined at runtime then this should 142 /// return true, otherwise instance variables will be accessed directly from 143 /// the structure. If this returns true then @defs is invalid for this 144 /// runtime and a warning should be generated. 145 virtual bool LateBoundIVars() const { return false; } 146 147 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 148 const Stmt &S) = 0; 149 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 150 const ObjCAtThrowStmt &S) = 0; 151 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 152 llvm::Value *AddrWeakObj) = 0; 153 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 154 llvm::Value *src, llvm::Value *dest) = 0; 155 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 156 llvm::Value *src, llvm::Value *dest) = 0; 157 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 158 llvm::Value *src, llvm::Value *dest) = 0; 159 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 160 llvm::Value *src, llvm::Value *dest) = 0; 161 162 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 163 QualType ObjectTy, 164 llvm::Value *BaseValue, 165 const ObjCIvarDecl *Ivar, 166 const FieldDecl *Field, 167 unsigned CVRQualifiers) = 0; 168 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 169 ObjCInterfaceDecl *Interface, 170 const ObjCIvarDecl *Ivar) = 0; 171}; 172 173/// Creates an instance of an Objective-C runtime class. 174//TODO: This should include some way of selecting which runtime to target. 175CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM); 176CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM); 177CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM); 178} 179} 180#endif 181