1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
//===--- GenObjC.h - Swift IR generation for Objective-C --------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file provides the private interface to Objective-C emission code.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_GENOBJC_H
#define SWIFT_IRGEN_GENOBJC_H
#include "swift/SIL/SILDeclRef.h"
namespace llvm {
class Type;
class Value;
}
namespace swift {
class CanType;
class FuncDecl;
struct SILDeclRef;
class SILFunction;
class SILType;
namespace irgen {
class Callee;
class CalleeInfo;
class ConstantArrayBuilder;
class IRGenFunction;
class IRGenModule;
/// The kind of message to send through the Objective-C runtime.
enum class ObjCMessageKind {
/// A normally-dispatched call.
Normal,
/// A call to a superclass method.
Super,
/// A call to a peer method.
Peer
};
/// Represents an ObjC method reference that will be invoked by a form of
/// objc_msgSend.
class ObjCMethod {
/// The SILDeclRef declaring the method.
SILDeclRef method;
/// For a bounded call, the static type that provides the lower bound for
/// the search. Null for unbounded calls that will look for the method in
/// the dynamic type of the object.
llvm::PointerIntPair<SILType, 1, bool> searchTypeAndSuper;
public:
ObjCMethod(SILDeclRef method, SILType searchType, bool startAtSuper)
: method(method), searchTypeAndSuper(searchType, startAtSuper)
{}
SILDeclRef getMethod() const { return method; }
SILType getSearchType() const { return searchTypeAndSuper.getPointer(); }
bool shouldStartAtSuper() const { return searchTypeAndSuper.getInt(); }
/// FIXME: Thunk down to a Swift function value?
llvm::Value *getExplosionValue(IRGenFunction &IGF) const {
llvm_unreachable("thunking unapplied objc method to swift function "
"not yet implemented");
}
/// Determine the kind of message that should be sent to this
/// method.
ObjCMessageKind getMessageKind() const {
// Determine the kind of message send to perform.
if (!getSearchType()) return ObjCMessageKind::Normal;
return shouldStartAtSuper()? ObjCMessageKind::Super
: ObjCMessageKind::Peer;
}
};
/// Prepare a callee for an Objective-C method.
Callee getObjCMethodCallee(IRGenFunction &IGF, const ObjCMethod &method,
llvm::Value *selfValue, CalleeInfo &&info);
/// Prepare a callee for an Objective-C method with the `objc_direct` attribute.
Callee getObjCDirectMethodCallee(CalleeInfo &&info, const FunctionPointer &fn,
llvm::Value *selfValue);
/// Emit a partial application of an Objective-C method to its 'self'
/// argument.
void emitObjCPartialApplication(IRGenFunction &IGF,
ObjCMethod method,
CanSILFunctionType origType,
CanSILFunctionType partialAppliedType,
llvm::Value *self,
SILType selfType,
Explosion &out);
/// Reclaim an autoreleased return value.
llvm::Value *emitObjCRetainAutoreleasedReturnValue(IRGenFunction &IGF,
llvm::Value *value);
/// Autorelease a return value.
llvm::Value *emitObjCAutoreleaseReturnValue(IRGenFunction &IGF,
llvm::Value *value);
struct ObjCMethodDescriptor {
llvm::Constant *selectorRef = nullptr;
llvm::Constant *typeEncoding = nullptr;
llvm::Constant *impl = nullptr;
SILFunction *silFunction = nullptr;
};
/// Build the components of an Objective-C method descriptor for the given
/// method or constructor implementation.
ObjCMethodDescriptor
emitObjCMethodDescriptorParts(IRGenModule &IGM,
AbstractFunctionDecl *method,
bool concrete);
/// Build the components of an Objective-C method descriptor for the given
/// property's method implementations.
ObjCMethodDescriptor emitObjCGetterDescriptorParts(IRGenModule &IGM,
VarDecl *property);
/// Build the components of an Objective-C method descriptor for the given
/// subscript's method implementations.
ObjCMethodDescriptor emitObjCGetterDescriptorParts(IRGenModule &IGM,
SubscriptDecl *property);
/// Build the components of an Objective-C method descriptor if the abstract
/// storage refers to a property or a subscript.
ObjCMethodDescriptor
emitObjCGetterDescriptorParts(IRGenModule &IGM,
AbstractStorageDecl *subscript);
/// Build the components of an Objective-C method descriptor for the given
/// property's method implementations.
ObjCMethodDescriptor emitObjCSetterDescriptorParts(IRGenModule &IGM,
VarDecl *property);
/// Build the components of an Objective-C method descriptor for the given
/// subscript's method implementations.
ObjCMethodDescriptor emitObjCSetterDescriptorParts(IRGenModule &IGM,
SubscriptDecl *property);
/// Build the components of an Objective-C method descriptor if the abstract
/// storage refers to a property or a subscript.
ObjCMethodDescriptor
emitObjCSetterDescriptorParts(IRGenModule &IGM,
AbstractStorageDecl *subscript);
/// Build an Objective-C method descriptor for the given method,
/// constructor, or destructor implementation.
void emitObjCMethodDescriptor(IRGenModule &IGM,
ConstantArrayBuilder &descriptors,
AbstractFunctionDecl *method);
/// Build an Objective-C method descriptor for the ivar initializer
/// or destroyer of a class (-.cxx_construct or -.cxx_destruct).
void emitObjCIVarInitDestroyDescriptor(IRGenModule &IGM,
ConstantArrayBuilder &descriptors,
ClassDecl *cd,
llvm::Function *impl,
bool isDestroyer);
/// Get the type encoding for an ObjC property.
void getObjCEncodingForPropertyType(IRGenModule &IGM, VarDecl *property,
std::string &s);
/// Produces extended encoding of ObjC block signature.
/// \returns the encoded type.
llvm::Constant *getBlockTypeExtendedEncoding(IRGenModule &IGM,
CanSILFunctionType invokeTy);
/// Produces extended encoding of method type.
/// \returns the encoded type.
llvm::Constant *getMethodTypeExtendedEncoding(IRGenModule &IGM,
AbstractFunctionDecl *method);
/// Build an Objective-C method descriptor for the given getter method.
void emitObjCGetterDescriptor(IRGenModule &IGM,
ConstantArrayBuilder &descriptors,
AbstractStorageDecl *storage);
/// Build an Objective-C method descriptor for the given setter method.
void emitObjCSetterDescriptor(IRGenModule &IGM,
ConstantArrayBuilder &descriptors,
AbstractStorageDecl *storage);
/// True if the FuncDecl requires an ObjC method descriptor.
bool requiresObjCMethodDescriptor(FuncDecl *method);
/// True if the ConstructorDecl requires an ObjC method descriptor.
bool requiresObjCMethodDescriptor(ConstructorDecl *constructor);
/// True if the VarDecl requires ObjC accessor methods and a property
/// descriptor.
bool requiresObjCPropertyDescriptor(IRGenModule &IGM,
VarDecl *property);
/// True if the SubscriptDecl requires ObjC accessor methods.
bool requiresObjCSubscriptDescriptor(IRGenModule &IGM,
SubscriptDecl *subscript);
/// Allocate an Objective-C object.
llvm::Value *emitObjCAllocObjectCall(IRGenFunction &IGF,
llvm::Value *classPtr,
SILType resultType);
} // end namespace irgen
} // end namespace swift
#endif
|