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