Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1 | //===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===// |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 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 |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/Basic/IdentifierTable.h" // Selector |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Argyrios Kyrtzidis | 8ef07c0 | 2008-06-01 21:23:24 +0000 | [diff] [blame] | 20 | #include <string> |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 21 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 22 | #include "CGBuilder.h" |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 23 | #include "CGCall.h" |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 24 | #include "CGValue.h" |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 26 | namespace llvm { |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 27 | class Constant; |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 28 | class Function; |
| 29 | class Module; |
| 30 | class StructLayout; |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 31 | class StructType; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 32 | class Type; |
| 33 | class Value; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | namespace clang { |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 37 | namespace CodeGen { |
| 38 | class CodeGenFunction; |
| 39 | } |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 41 | class FieldDecl; |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 42 | class ObjCAtTryStmt; |
| 43 | class ObjCAtThrowStmt; |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 44 | class ObjCAtSynchronizedStmt; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 45 | class ObjCContainerDecl; |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 46 | class ObjCCategoryImplDecl; |
| 47 | class ObjCImplementationDecl; |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 48 | class ObjCInterfaceDecl; |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 49 | class ObjCMessageExpr; |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 50 | class ObjCMethodDecl; |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 51 | class ObjCProtocolDecl; |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 52 | class Selector; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 53 | class ObjCIvarDecl; |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 54 | class ObjCStringLiteral; |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 56 | namespace CodeGen { |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 57 | class CodeGenModule; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 58 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 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. |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 63 | class CGObjCRuntime { |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 64 | public: |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 65 | // Utility functions for unified ivar access. These need to |
| 66 | // eventually be folded into other places (the structure layout |
| 67 | // code). |
| 68 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 69 | protected: |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 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! |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 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); |
Daniel Dunbar | 58bf610 | 2008-08-11 16:50:21 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 84 | public: |
| 85 | virtual ~CGObjCRuntime(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 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(). |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 94 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 95 | Selector Sel) = 0; |
| 96 | |
| 97 | /// Generate a constant string object. |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 98 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0; |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 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; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 106 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 107 | /// Generate an Objective-C message send operation. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 108 | virtual CodeGen::RValue |
| 109 | GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 110 | QualType ResultType, |
| 111 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 112 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 113 | bool IsClassMessage, |
| 114 | const CallArgList &CallArgs) = 0; |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 116 | /// Generate an Objective-C message send operation to the super |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 117 | /// class initiated in a method for Class and with the given Self |
| 118 | /// object. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 119 | virtual CodeGen::RValue |
| 120 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 121 | QualType ResultType, |
| 122 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 123 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 124 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 125 | llvm::Value *Self, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 126 | bool IsClassMessage, |
| 127 | const CallArgList &CallArgs) = 0; |
Daniel Dunbar | 98c5ead | 2008-08-12 05:08:18 +0000 | [diff] [blame] | 128 | |
| 129 | /// Emit the code to return the named protocol as an object, as in a |
| 130 | /// @protocol expression. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 131 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 132 | const ObjCProtocolDecl *OPD) = 0; |
Daniel Dunbar | 98c5ead | 2008-08-12 05:08:18 +0000 | [diff] [blame] | 133 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 134 | /// Generate the named protocol. Protocols contain method metadata but no |
| 135 | /// implementations. |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 136 | virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0; |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 137 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 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. |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 145 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 146 | const ObjCContainerDecl *CD) = 0; |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 147 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 148 | /// Return the runtime function for getting properties. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 149 | virtual llvm::Constant *GetPropertyGetFunction() = 0; |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 150 | |
| 151 | /// Return the runtime function for setting properties. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 152 | virtual llvm::Constant *GetPropertySetFunction() = 0; |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 154 | /// GetClass - Return a reference to the class for the given |
| 155 | /// interface decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 156 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 157 | const ObjCInterfaceDecl *OID) = 0; |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 158 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 159 | /// EnumerationMutationFunction - Return the function that's called by the |
| 160 | /// compiler when a mutation is detected during foreach iteration. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 161 | virtual llvm::Constant *EnumerationMutationFunction() = 0; |
Daniel Dunbar | 6bf2ae0 | 2009-04-21 00:49:20 +0000 | [diff] [blame] | 162 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 163 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 164 | const Stmt &S) = 0; |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 165 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 166 | const ObjCAtThrowStmt &S) = 0; |
Daniel Dunbar | 6bf2ae0 | 2009-04-21 00:49:20 +0000 | [diff] [blame] | 167 | virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
| 168 | llvm::Value *AddrWeakObj) = 0; |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 169 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 170 | llvm::Value *src, llvm::Value *dest) = 0; |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 171 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 172 | llvm::Value *src, llvm::Value *dest) = 0; |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 173 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 174 | llvm::Value *src, llvm::Value *dest) = 0; |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 175 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 176 | llvm::Value *src, llvm::Value *dest) = 0; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 177 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 178 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 179 | QualType ObjectTy, |
| 180 | llvm::Value *BaseValue, |
| 181 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 182 | unsigned CVRQualifiers) = 0; |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 183 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 184 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 185 | const ObjCIvarDecl *Ivar) = 0; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 188 | /// Creates an instance of an Objective-C runtime class. |
| 189 | //TODO: This should include some way of selecting which runtime to target. |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 190 | CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM); |
| 191 | CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 192 | CGObjCRuntime *CreateMacNonFragileABIObjCRuntime(CodeGenModule &CGM); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | #endif |