Skip to content

Commit 0082682

Browse files
committed
IRGen: Refactor getObjCEncodingForMethod and getObjectEncodingFromClangNode into one
NFC.
1 parent f6662a8 commit 0082682

File tree

1 file changed

+41
-50
lines changed

1 file changed

+41
-50
lines changed

lib/IRGen/GenObjC.cpp

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,22 +1100,6 @@ static llvm::Constant *getObjCEncodingForTypes(IRGenModule &IGM,
11001100
return IGM.getAddrOfGlobalString(encodingString);
11011101
}
11021102

1103-
static llvm::Constant *getObjCEncodingForMethodType(IRGenModule &IGM,
1104-
CanSILFunctionType fnType,
1105-
bool useExtendedEncoding) {
1106-
// Get the inputs without 'self'.
1107-
auto inputs = fnType->getParameters().drop_back();
1108-
1109-
// Include the encoding for 'self' and '_cmd'.
1110-
llvm::SmallString<8> specialParams;
1111-
specialParams += "@0:";
1112-
auto ptrSize = IGM.getPointerSize().getValue();
1113-
specialParams += llvm::itostr(ptrSize);
1114-
GenericContextScope scope(IGM, fnType->getInvocationGenericSignature());
1115-
return getObjCEncodingForTypes(IGM, fnType, inputs, specialParams,
1116-
ptrSize * 2, useExtendedEncoding);
1117-
}
1118-
11191103
static llvm::Constant *
11201104
getObjectEncodingFromClangNode(IRGenModule &IGM, Decl *d,
11211105
bool useExtendedEncoding) {
@@ -1139,6 +1123,31 @@ getObjectEncodingFromClangNode(IRGenModule &IGM, Decl *d,
11391123
return nullptr;
11401124
}
11411125

1126+
static llvm::Constant *getObjCEncodingForMethod(IRGenModule &IGM,
1127+
CanSILFunctionType fnType,
1128+
bool useExtendedEncoding,
1129+
Decl *optionalDecl) {
1130+
// Use the decl's ClangNode to get the encoding if possible.
1131+
if (optionalDecl) {
1132+
if (auto *enc = getObjectEncodingFromClangNode(IGM, optionalDecl,
1133+
useExtendedEncoding)) {
1134+
return enc;
1135+
}
1136+
}
1137+
1138+
// Get the inputs without 'self'.
1139+
auto inputs = fnType->getParameters().drop_back();
1140+
1141+
// Include the encoding for 'self' and '_cmd'.
1142+
llvm::SmallString<8> specialParams;
1143+
specialParams += "@0:";
1144+
auto ptrSize = IGM.getPointerSize().getValue();
1145+
specialParams += llvm::itostr(ptrSize);
1146+
GenericContextScope scope(IGM, fnType->getInvocationGenericSignature());
1147+
return getObjCEncodingForTypes(IGM, fnType, inputs, specialParams,
1148+
ptrSize * 2, useExtendedEncoding);
1149+
}
1150+
11421151
/// Emit the components of an Objective-C method descriptor: its selector,
11431152
/// type encoding, and IMP pointer.
11441153
ObjCMethodDescriptor
@@ -1151,18 +1160,13 @@ irgen::emitObjCMethodDescriptorParts(IRGenModule &IGM,
11511160
/// The first element is the selector.
11521161
descriptor.selectorRef = IGM.getAddrOfObjCMethodName(selector.str());
11531162

1154-
if (auto e =
1155-
getObjectEncodingFromClangNode(IGM, method, false /*extended*/)) {
1156-
descriptor.typeEncoding = e;
1157-
} else {
1158-
/// The second element is the method signature. A method signature is made
1159-
/// of the return type @encoding and every parameter type @encoding, glued
1160-
/// with numbers that used to represent stack offsets for each of these
1161-
/// elements.
1162-
CanSILFunctionType methodType = getObjCMethodType(IGM, method);
1163-
descriptor.typeEncoding =
1164-
getObjCEncodingForMethodType(IGM, methodType, /*extended*/ false);
1165-
}
1163+
/// The second element is the method signature. A method signature is made
1164+
/// of the return type @encoding and every parameter type @encoding, glued
1165+
/// with numbers that used to represent stack offsets for each of these
1166+
/// elements.
1167+
CanSILFunctionType methodType = getObjCMethodType(IGM, method);
1168+
descriptor.typeEncoding =
1169+
getObjCEncodingForMethod(IGM, methodType, /*extended*/ false, method);
11661170

11671171
/// The third element is the method implementation pointer.
11681172
if (!concrete) {
@@ -1222,15 +1226,11 @@ irgen::emitObjCGetterDescriptorParts(IRGenModule &IGM,
12221226
ObjCMethodDescriptor descriptor{};
12231227
descriptor.selectorRef = IGM.getAddrOfObjCMethodName(getterSel.str());
12241228

1225-
if (auto e =
1226-
getObjectEncodingFromClangNode(IGM, subscript, false /*extended*/)) {
1227-
descriptor.typeEncoding = e;
1228-
} else {
1229-
auto methodTy =
1230-
getObjCMethodType(IGM, subscript->getOpaqueAccessor(AccessorKind::Get));
1231-
descriptor.typeEncoding = getObjCEncodingForMethodType(IGM, methodTy,
1232-
/*extended*/ false);
1233-
}
1229+
auto methodTy =
1230+
getObjCMethodType(IGM, subscript->getOpaqueAccessor(AccessorKind::Get));
1231+
descriptor.typeEncoding =
1232+
getObjCEncodingForMethod(IGM, methodTy,
1233+
/*extended*/ false, subscript);
12341234

12351235
descriptor.silFunction = nullptr;
12361236
descriptor.impl = getObjCGetterPointer(IGM, subscript,
@@ -1303,13 +1303,9 @@ irgen::emitObjCSetterDescriptorParts(IRGenModule &IGM,
13031303
descriptor.selectorRef = IGM.getAddrOfObjCMethodName(setterSel.str());
13041304
auto methodTy = getObjCMethodType(IGM,
13051305
subscript->getOpaqueAccessor(AccessorKind::Set));
1306-
if (auto e =
1307-
getObjectEncodingFromClangNode(IGM, subscript, false /*extended*/)) {
1308-
descriptor.typeEncoding = e;
1309-
} else {
1310-
descriptor.typeEncoding = getObjCEncodingForMethodType(IGM, methodTy,
1311-
/*extended*/ false);
1312-
}
1306+
descriptor.typeEncoding =
1307+
getObjCEncodingForMethod(IGM, methodTy,
1308+
/*extended*/ false, subscript);
13131309
descriptor.silFunction = nullptr;
13141310
descriptor.impl = getObjCSetterPointer(IGM, subscript,
13151311
descriptor.silFunction);
@@ -1415,13 +1411,8 @@ void irgen::emitObjCIVarInitDestroyDescriptor(
14151411
llvm::Constant *
14161412
irgen::getMethodTypeExtendedEncoding(IRGenModule &IGM,
14171413
AbstractFunctionDecl *method) {
1418-
// Use the clang node's encoding if there is a clang node.
1419-
if (auto e = getObjectEncodingFromClangNode(IGM, method, true /*extended*/)) {
1420-
return e;
1421-
}
1422-
14231414
CanSILFunctionType methodType = getObjCMethodType(IGM, method);
1424-
return getObjCEncodingForMethodType(IGM, methodType, true/*Extended*/);
1415+
return getObjCEncodingForMethod(IGM, methodType, true /*Extended*/, method);
14251416
}
14261417

14271418
llvm::Constant *

0 commit comments

Comments
 (0)