[Concurrency] Move removal of leading "get" for async imports.

Name adjustments should be performed by omitNeedlessWords(), not the
early classifcation of async imports.
This commit is contained in:
Doug Gregor
2020-10-21 21:48:52 -07:00
parent e1852956a0
commit 24ea8becc8
6 changed files with 32 additions and 31 deletions

View File

@@ -442,6 +442,8 @@ public:
/// ///
/// \param allPropertyNames The set of property names in the enclosing context. /// \param allPropertyNames The set of property names in the enclosing context.
/// ///
/// \param isAsync Whether this is a function imported as 'async'.
///
/// \param scratch Scratch space that will be used for modifications beyond /// \param scratch Scratch space that will be used for modifications beyond
/// just chopping names. /// just chopping names.
/// ///
@@ -455,6 +457,7 @@ bool omitNeedlessWords(StringRef &baseName,
bool returnsSelf, bool returnsSelf,
bool isProperty, bool isProperty,
const InheritedNameSet *allPropertyNames, const InheritedNameSet *allPropertyNames,
bool isAsync,
StringScratchSpace &scratch); StringScratchSpace &scratch);
} // end namespace swift } // end namespace swift

View File

@@ -1220,6 +1220,7 @@ bool swift::omitNeedlessWords(StringRef &baseName,
bool returnsSelf, bool returnsSelf,
bool isProperty, bool isProperty,
const InheritedNameSet *allPropertyNames, const InheritedNameSet *allPropertyNames,
bool isAsync,
StringScratchSpace &scratch) { StringScratchSpace &scratch) {
bool anyChanges = false; bool anyChanges = false;
OmissionTypeName resultType = returnsSelf ? contextType : givenResultType; OmissionTypeName resultType = returnsSelf ? contextType : givenResultType;
@@ -1287,6 +1288,14 @@ bool swift::omitNeedlessWords(StringRef &baseName,
} }
} }
// If the base name of a method imported as "async" starts with the word
// "get", drop the "get".
if (isAsync && camel_case::getFirstWord(baseName) == "get" &&
baseName.size() > 3) {
baseName = baseName.substr(3);
anyChanges = true;
}
// If needed, split the base name. // If needed, split the base name.
if (!argNames.empty() && if (!argNames.empty() &&
splitBaseName(baseName, argNames[0], paramTypes[0], firstParamName)) splitBaseName(baseName, argNames[0], paramTypes[0], firstParamName))

View File

@@ -448,7 +448,7 @@ private:
baseName = humbleBaseName.userFacingName(); baseName = humbleBaseName.userFacingName();
bool didOmit = bool didOmit =
omitNeedlessWords(baseName, argStrs, "", "", "", paramTypeNames, false, omitNeedlessWords(baseName, argStrs, "", "", "", paramTypeNames, false,
false, nullptr, scratch); false, nullptr, false, scratch);
SmallVector<Identifier, 8> argLabels; SmallVector<Identifier, 8> argLabels;
for (auto str : argStrs) for (auto str : argStrs)
argLabels.push_back(getIdentifier(str)); argLabels.push_back(getIdentifier(str));

View File

@@ -804,7 +804,7 @@ static bool omitNeedlessWordsInFunctionName(
ArrayRef<const clang::ParmVarDecl *> params, clang::QualType resultType, ArrayRef<const clang::ParmVarDecl *> params, clang::QualType resultType,
const clang::DeclContext *dc, const SmallBitVector &nonNullArgs, const clang::DeclContext *dc, const SmallBitVector &nonNullArgs,
Optional<unsigned> errorParamIndex, bool returnsSelf, bool isInstanceMethod, Optional<unsigned> errorParamIndex, bool returnsSelf, bool isInstanceMethod,
NameImporter &nameImporter) { Optional<unsigned> completionHandlerIndex, NameImporter &nameImporter) {
clang::ASTContext &clangCtx = nameImporter.getClangContext(); clang::ASTContext &clangCtx = nameImporter.getClangContext();
// Collect the parameter type names. // Collect the parameter type names.
@@ -817,10 +817,6 @@ static bool omitNeedlessWordsInFunctionName(
if (i == 0) if (i == 0)
firstParamName = param->getName(); firstParamName = param->getName();
// Determine the number of parameters.
unsigned numParams = params.size();
if (errorParamIndex) --numParams;
bool isLastParameter bool isLastParameter
= (i == params.size() - 1) || = (i == params.size() - 1) ||
(i == params.size() - 2 && (i == params.size() - 2 &&
@@ -858,7 +854,8 @@ static bool omitNeedlessWordsInFunctionName(
getClangTypeNameForOmission(clangCtx, resultType), getClangTypeNameForOmission(clangCtx, resultType),
getClangTypeNameForOmission(clangCtx, contextType), getClangTypeNameForOmission(clangCtx, contextType),
paramTypes, returnsSelf, /*isProperty=*/false, paramTypes, returnsSelf, /*isProperty=*/false,
allPropertyNames, nameImporter.getScratch()); allPropertyNames, completionHandlerIndex.hasValue(),
nameImporter.getScratch());
} }
/// Prepare global name for importing onto a swift_newtype. /// Prepare global name for importing onto a swift_newtype.
@@ -1189,7 +1186,6 @@ Optional<ForeignAsyncConvention::Info>
NameImporter::considerAsyncImport( NameImporter::considerAsyncImport(
const clang::ObjCMethodDecl *clangDecl, const clang::ObjCMethodDecl *clangDecl,
StringRef &baseName, StringRef &baseName,
SmallVectorImpl<char> &baseNameScratch,
SmallVectorImpl<StringRef> &paramNames, SmallVectorImpl<StringRef> &paramNames,
ArrayRef<const clang::ParmVarDecl *> params, ArrayRef<const clang::ParmVarDecl *> params,
bool isInitializer, bool hasCustomName, bool isInitializer, bool hasCustomName,
@@ -1225,17 +1221,6 @@ NameImporter::considerAsyncImport(
return None; return None;
} }
// If there's no custom name, and the base name starts with "get", drop the
// get.
if (!hasCustomName) {
StringRef currentBaseName = newBaseName ? *newBaseName : baseName;
if (currentBaseName.size() > 3 &&
camel_case::getFirstWord(currentBaseName) == "get") {
newBaseName = camel_case::toLowercaseInitialisms(
currentBaseName.substr(3), baseNameScratch);
}
}
// Used for returns once we've determined that the method cannot be // Used for returns once we've determined that the method cannot be
// imported as async, even though it has what looks like a completion handler // imported as async, even though it has what looks like a completion handler
// parameter. // parameter.
@@ -1473,7 +1458,6 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
} }
// If we have a swift_name attribute, use that. // If we have a swift_name attribute, use that.
SmallString<32> asyncBaseNameScratch;
if (auto *nameAttr = findSwiftNameAttr(D, version)) { if (auto *nameAttr = findSwiftNameAttr(D, version)) {
bool skipCustomName = false; bool skipCustomName = false;
@@ -1552,9 +1536,9 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
if (version.supportsConcurrency()) { if (version.supportsConcurrency()) {
if (auto asyncInfo = considerAsyncImport( if (auto asyncInfo = considerAsyncImport(
method, parsedName.BaseName, asyncBaseNameScratch, method, parsedName.BaseName, parsedName.ArgumentLabels,
parsedName.ArgumentLabels, params, isInitializer, params, isInitializer, /*hasCustomName=*/true,
/*hasCustomName=*/true, result.getErrorInfo())) { result.getErrorInfo())) {
result.info.hasAsyncInfo = true; result.info.hasAsyncInfo = true;
result.info.asyncInfo = *asyncInfo; result.info.asyncInfo = *asyncInfo;
@@ -1829,9 +1813,8 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
if (version.supportsConcurrency() && if (version.supportsConcurrency() &&
result.info.accessorKind == ImportedAccessorKind::None) { result.info.accessorKind == ImportedAccessorKind::None) {
if (auto asyncInfo = considerAsyncImport( if (auto asyncInfo = considerAsyncImport(
objcMethod, baseName, asyncBaseNameScratch, objcMethod, baseName, argumentNames, params, isInitializer,
argumentNames, params, isInitializer, /*hasCustomName=*/false, /*hasCustomName=*/false, result.getErrorInfo())) {
result.getErrorInfo())) {
result.info.hasAsyncInfo = true; result.info.hasAsyncInfo = true;
result.info.asyncInfo = *asyncInfo; result.info.asyncInfo = *asyncInfo;
} }
@@ -1971,7 +1954,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
(void)omitNeedlessWords(baseName, {}, "", propertyTypeName, (void)omitNeedlessWords(baseName, {}, "", propertyTypeName,
contextTypeName, {}, /*returnsSelf=*/false, contextTypeName, {}, /*returnsSelf=*/false,
/*isProperty=*/true, allPropertyNames, /*isProperty=*/true, allPropertyNames,
scratch); /*isAsync=*/false, scratch);
} }
} }
@@ -1983,7 +1966,12 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
result.getErrorInfo() result.getErrorInfo()
? Optional<unsigned>(result.getErrorInfo()->ErrorParameterIndex) ? Optional<unsigned>(result.getErrorInfo()->ErrorParameterIndex)
: None, : None,
method->hasRelatedResultType(), method->isInstanceMethod(), *this); method->hasRelatedResultType(), method->isInstanceMethod(),
result.getAsyncInfo().map(
[](const ForeignAsyncConvention::Info &info) {
return info.CompletionHandlerParamIndex;
}),
*this);
} }
// If the result is a value, lowercase it. // If the result is a value, lowercase it.

View File

@@ -456,7 +456,6 @@ private:
Optional<ForeignAsyncConvention::Info> Optional<ForeignAsyncConvention::Info>
considerAsyncImport(const clang::ObjCMethodDecl *clangDecl, considerAsyncImport(const clang::ObjCMethodDecl *clangDecl,
StringRef &baseName, StringRef &baseName,
SmallVectorImpl<char> &baseNameScratch,
SmallVectorImpl<StringRef> &paramNames, SmallVectorImpl<StringRef> &paramNames,
ArrayRef<const clang::ParmVarDecl *> params, ArrayRef<const clang::ParmVarDecl *> params,
bool isInitializer, bool hasCustomName, bool isInitializer, bool hasCustomName,

View File

@@ -4867,7 +4867,8 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
getTypeNameForOmission(resultType), getTypeNameForOmission(resultType),
getTypeNameForOmission(contextType), getTypeNameForOmission(contextType),
paramTypes, returnsSelf, false, paramTypes, returnsSelf, false,
/*allPropertyNames=*/nullptr, scratch)) /*allPropertyNames=*/nullptr,
afd->hasAsync(), scratch))
return None; return None;
/// Retrieve a replacement identifier. /// Retrieve a replacement identifier.
@@ -4922,7 +4923,8 @@ Optional<Identifier> TypeChecker::omitNeedlessWords(VarDecl *var) {
OmissionTypeName contextTypeName = getTypeNameForOmission(contextType); OmissionTypeName contextTypeName = getTypeNameForOmission(contextType);
if (::omitNeedlessWords(name, { }, "", typeName, contextTypeName, { }, if (::omitNeedlessWords(name, { }, "", typeName, contextTypeName, { },
/*returnsSelf=*/false, true, /*returnsSelf=*/false, true,
/*allPropertyNames=*/nullptr, scratch)) { /*allPropertyNames=*/nullptr, /*isAsync=*/false,
scratch)) {
return Context.getIdentifier(name); return Context.getIdentifier(name);
} }