Skip to content

[c++-interop] Providing information about enum types from inferDefaultArgument #59421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5744,6 +5744,10 @@ class ParamDecl : public VarDecl {
Bits.ParamDecl.defaultArgumentKind = static_cast<unsigned>(K);
}

void setDefaultArgumentKind(ArgumentAttrs K) {
setDefaultArgumentKind(K.argumentKind);
}

bool isNoImplicitCopy() const {
return getAttrs().hasAttribute<NoImplicitCopyAttr>();
}
Expand Down
36 changes: 36 additions & 0 deletions include/swift/AST/DefaultArgumentKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#ifndef SWIFT_DEFAULTARGUMENTKIND_H
#define SWIFT_DEFAULTARGUMENTKIND_H

#include "llvm/ADT/StringRef.h"
#include <cstdint>
#include <string>

namespace llvm {
class StringRef;
Expand Down Expand Up @@ -52,6 +54,40 @@ enum class DefaultArgumentKind : uint8_t {
};
enum { NumDefaultArgumentKindBits = 4 };

struct ArgumentAttrs {
DefaultArgumentKind argumentKind;
bool isUnavailableInSwift = false;
llvm::StringRef CXXOptionsEnumName = "";

ArgumentAttrs(DefaultArgumentKind argumentKind,
bool isUnavailableInSwift = false,
llvm::StringRef CXXOptionsEnumName = "")
: argumentKind(argumentKind), isUnavailableInSwift(isUnavailableInSwift),
CXXOptionsEnumName(CXXOptionsEnumName) {}

bool operator !=(const DefaultArgumentKind &rhs) const {
return argumentKind != rhs;
}

bool operator==(const DefaultArgumentKind &rhs) const {
return argumentKind == rhs;
}

bool hasDefaultArg() const {
return argumentKind != DefaultArgumentKind::None;
}

bool hasAlternateCXXOptionsEnumName() const {
return !CXXOptionsEnumName.empty() && isUnavailableInSwift;
}

llvm::StringRef getAlternateCXXOptionsEnumName() const {
assert(hasAlternateCXXOptionsEnumName() &&
"Expected a C++ Options type for C++-Interop but found none.");
return CXXOptionsEnumName;
}
};

} // end namespace swift

#endif // LLVM_SWIFT_DEFAULTARGUMENTKIND_H
Expand Down
12 changes: 7 additions & 5 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,16 +869,18 @@ static bool omitNeedlessWordsInFunctionName(
StringRef argumentName;
if (i < argumentNames.size())
argumentName = argumentNames[i];
bool hasDefaultArg =
auto argumentAttrs =
ClangImporter::Implementation::inferDefaultArgument(
param->getType(),
getParamOptionality(param, !nonNullArgs.empty() && nonNullArgs[i]),
nameImporter.getIdentifier(baseName), argumentName, i == 0,
isLastParameter, nameImporter) != DefaultArgumentKind::None;
isLastParameter, nameImporter);

paramTypes.push_back(getClangTypeNameForOmission(clangCtx,
param->getOriginalType())
.withDefaultArgument(hasDefaultArg));
paramTypes.push_back(
(argumentAttrs.hasAlternateCXXOptionsEnumName()
? OmissionTypeName(argumentAttrs.getAlternateCXXOptionsEnumName())
: getClangTypeNameForOmission(clangCtx, param->getOriginalType()))
.withDefaultArgument(argumentAttrs.hasDefaultArg()));
}

// Find the property names.
Expand Down
28 changes: 24 additions & 4 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "swift/ABI/MetadataValues.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DefaultArgumentKind.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsClangImporter.h"
#include "swift/AST/ExistentialLayout.h"
Expand Down Expand Up @@ -2410,7 +2411,7 @@ static bool isObjCMethodResultAudited(const clang::Decl *decl) {
decl->hasAttr<clang::ObjCReturnsInnerPointerAttr>());
}

DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument(
ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
clang::QualType type, OptionalTypeKind clangOptionality,
DeclBaseName baseName, StringRef argumentLabel, bool isFirstParameter,
bool isLastParameter, NameImporter &nameImporter) {
Expand Down Expand Up @@ -2455,10 +2456,29 @@ DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument(
// If we've taken this branch it means we have an enum type, and it is
// likely an integer or NSInteger that is being used by NS/CF_OPTIONS to
// behave like a C enum in the presence of C++.
auto enumName = typedefType->getDecl()->getDeclName().getAsString();
auto enumName = typedefType->getDecl()->getName();
ArgumentAttrs argumentAttrs(DefaultArgumentKind::None, true, enumName);
for (auto word : llvm::reverse(camel_case::getWords(enumName))) {
if (camel_case::sameWordIgnoreFirstCase(word, "options"))
return DefaultArgumentKind::EmptyArray;
if (camel_case::sameWordIgnoreFirstCase(word, "options")) {
argumentAttrs.argumentKind = DefaultArgumentKind::EmptyArray;
return argumentAttrs;
}
if (camel_case::sameWordIgnoreFirstCase(word, "units"))
return argumentAttrs;
if (camel_case::sameWordIgnoreFirstCase(word, "domain"))
return argumentAttrs;
if (camel_case::sameWordIgnoreFirstCase(word, "action"))
return argumentAttrs;
if (camel_case::sameWordIgnoreFirstCase(word, "controlevents"))
return argumentAttrs;
if (camel_case::sameWordIgnoreFirstCase(word, "state"))
return argumentAttrs;
if (camel_case::sameWordIgnoreFirstCase(word, "unit"))
return argumentAttrs;
if (camel_case::sameWordIgnoreFirstCase(word, "scrollposition"))
return argumentAttrs;
if (camel_case::sameWordIgnoreFirstCase(word, "edge"))
return argumentAttrs;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation

/// Attempt to infer a default argument for a parameter with the
/// given Clang \c type, \c baseName, and optionality.
static DefaultArgumentKind
static ArgumentAttrs
inferDefaultArgument(clang::QualType type, OptionalTypeKind clangOptionality,
DeclBaseName baseName, StringRef argumentLabel,
bool isFirstParameter, bool isLastParameter,
Expand Down
30 changes: 30 additions & 0 deletions test/Interop/Cxx/enum/Inputs/c-enums-withOptions-omit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,33 @@ enum : NSEnumerationOptions { NSEnumerationConcurrent, NSEnumerationReverse };
@interface NSSet
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts ;
@end

typedef int __attribute__((availability(swift, unavailable))) NSOrderedCollectionDifferenceCalculationOptions;
enum : NSOrderedCollectionDifferenceCalculationOptions {
NSOrderedCollectionDifferenceCalculationOptions1,
NSOrderedCollectionDifferenceCalculationOptions2
};

typedef int __attribute__((availability(swift, unavailable))) NSCalendarUnit;
enum : NSCalendarUnit { NSCalendarUnit1, NSCalendarUnit2 };

typedef int __attribute__((availability(swift, unavailable))) NSSearchPathDomainMask;
enum : NSSearchPathDomainMask { NSSearchPathDomainMask1, NSSearchPathDomainMask2 };

typedef int __attribute__((availability(swift, unavailable))) NSControlCharacterAction;
enum : NSControlCharacterAction { NSControlCharacterAction1, NSControlCharacterAction2 };

typedef int __attribute__((availability(swift, unavailable))) UIControlState;
enum : UIControlState { UIControlState1, UIControlState2 };

typedef int __attribute__((availability(swift, unavailable))) UITableViewCellStateMask;
enum : UITableViewCellStateMask { UITableViewCellStateMask1, UITableViewCellStateMask2 };

@interface TestsForEnhancedOmitNeedlessWords
- (void)differenceFromArray:(int)other withOptions:(NSOrderedCollectionDifferenceCalculationOptions)options ;
- (unsigned)minimumRangeOfUnit:(NSCalendarUnit)unit;
- (unsigned)URLForDirectory:(unsigned)directory inDomain:(NSSearchPathDomainMask)domain ;
- (unsigned)layoutManager:(unsigned)layoutManager shouldUseAction:(NSControlCharacterAction)action ;
- (void)setBackButtonBackgroundImage:(unsigned)backgroundImage forState:(UIControlState)state ;
- (void)willTransitionToState:(UITableViewCellStateMask)state ;
@end
50 changes: 50 additions & 0 deletions test/Interop/Cxx/enum/c-enums-withOptions-omit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,53 @@ import CenumsWithOptionsOmit
// CHECK-NEXT: class func enumerateObjects(options
// CHECK-NEXT: func enumerateObjects(options
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "enumerateObjects(options:)")

// CHECK: class TestsForEnhancedOmitNeedlessWords {

// Tests for withOptions -> 'with options'
// CHECK-NEXT: class func difference(fromArray other: Int32, with options: NSOrderedCollectionDifferenceCalculationOptions = [])
// CHECK-NEXT: func difference(fromArray other: Int32, with options: NSOrderedCollectionDifferenceCalculationOptions = [])
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "difference(fromArray:with:)")
// CHECK-NEXT: class func differenceFromArray(_ other: Int32, withOptions options: NSOrderedCollectionDifferenceCalculationOptions = [])
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "difference(fromArray:with:)")
// CHECK-NEXT: func differenceFromArray(_ other: Int32, withOptions options: NSOrderedCollectionDifferenceCalculationOptions = [])

// Tests for ofUnit -> 'of unit'
// CHECK-NEXT: class func minimumRange(of unit: NSCalendarUnit) -> UInt32
// CHECK-NEXT: func minimumRange(of unit: NSCalendarUnit) -> UInt32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "minimumRange(of:)")
// CHECK-NEXT: class func minimumRangeOfUnit(_ unit: NSCalendarUnit) -> UInt32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "minimumRange(of:)")
// CHECK-NEXT: func minimumRangeOfUnit(_ unit: NSCalendarUnit) -> UInt32

// Tests for inDomain -> 'in domain'
// CHECK-NEXT: class func url(forDirectory directory: UInt32, in domain: NSSearchPathDomainMask) -> UInt32
// CHECK-NEXT: func url(forDirectory directory: UInt32, in domain: NSSearchPathDomainMask) -> UInt32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "url(forDirectory:in:)")
// CHECK-NEXT: class func URLForDirectory(_ directory: UInt32, inDomain domain: NSSearchPathDomainMask) -> UInt32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "url(forDirectory:in:)")
// CHECK-NEXT: func URLForDirectory(_ directory: UInt32, inDomain domain: NSSearchPathDomainMask) -> UInt32

// Tests for shouldUseAction -> 'shouldUse action'
// CHECK-NEXT: class func layoutManager(_ layoutManager: UInt32, shouldUse action: NSControlCharacterAction) -> UInt32
// CHECK-NEXT: func layoutManager(_ layoutManager: UInt32, shouldUse action: NSControlCharacterAction) -> UInt32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "layoutManager(_:shouldUse:)")
// CHECK-NEXT: class func layoutManager(_ layoutManager: UInt32, shouldUseAction action: NSControlCharacterAction) -> UInt32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "layoutManager(_:shouldUse:)")
// CHECK-NEXT: func layoutManager(_ layoutManager: UInt32, shouldUseAction action: NSControlCharacterAction) -> UInt32

// Tests for forState -> 'for state'
// CHECK-NEXT: class func setBackButtonBackgroundImage(_ backgroundImage: UInt32, for state: UIControlState)
// CHECK-NEXT: func setBackButtonBackgroundImage(_ backgroundImage: UInt32, for state: UIControlState)
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "setBackButtonBackgroundImage(_:for:)")
// CHECK-NEXT: class func setBackButtonBackgroundImage(_ backgroundImage: UInt32, forState state: UIControlState)
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "setBackButtonBackgroundImage(_:for:)")
// CHECK-NEXT: func setBackButtonBackgroundImage(_ backgroundImage: UInt32, forState state: UIControlState)

// Tests for toState -> 'to state'
// CHECK-NEXT: class func willTransition(to state: UITableViewCellStateMask)
// CHECK-NEXT: func willTransition(to state: UITableViewCellStateMask)
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "willTransition(to:)")
// CHECK-NEXT: class func willTransitionToState(_ state: UITableViewCellStateMask)
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "willTransition(to:)")
// CHECK-NEXT: func willTransitionToState(_ state: UITableViewCellStateMask)