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