@@ -3598,11 +3598,6 @@ namespace {
3598
3598
CtorInitializerKind initKind,
3599
3599
Optional<ImportedName> correctSwiftName);
3600
3600
3601
- Decl *importGlobalAsMethod (const clang::FunctionDecl *decl, DeclName name,
3602
- DeclContext *dc, Optional<unsigned > selfIdx,
3603
- Optional<ImportedName> correctSwiftName,
3604
- Optional<AccessorInfo> accessorInfo);
3605
-
3606
3601
// / Create an implicit property given the imported name of one of
3607
3602
// / the accessors.
3608
3603
VarDecl *getImplicitProperty (ImportedName importedName,
@@ -3650,7 +3645,12 @@ namespace {
3650
3645
return nullptr ;
3651
3646
3652
3647
DeclName name = accessorInfo ? DeclName () : importedName.getDeclName ();
3648
+ auto selfIdx = importedName.getSelfIndex ();
3653
3649
3650
+ FuncDecl *result = nullptr ;
3651
+ ImportedType importedType;
3652
+ bool selfIsInOut = false ;
3653
+ ParameterList *bodyParams = nullptr ;
3654
3654
if (!dc->isModuleScopeContext () && !isa<clang::CXXMethodDecl>(decl)) {
3655
3655
// Handle initializers.
3656
3656
if (name.getBaseName () == DeclBaseName::createConstructor ()) {
@@ -3660,23 +3660,78 @@ namespace {
3660
3660
correctSwiftName);
3661
3661
}
3662
3662
3663
- // Everything else is a method.
3664
- return importGlobalAsMethod (decl, name, dc,
3665
- importedName.getSelfIndex (),
3666
- correctSwiftName, accessorInfo);
3667
- }
3663
+ if (dc->getSelfProtocolDecl () && !selfIdx) {
3664
+ // FIXME: source location...
3665
+ Impl.SwiftContext .Diags .diagnose ({}, diag::swift_name_protocol_static,
3666
+ /* isInit=*/ false );
3667
+ Impl.SwiftContext .Diags .diagnose ({}, diag::note_while_importing,
3668
+ decl->getName ());
3669
+ return nullptr ;
3670
+ }
3668
3671
3669
- // Import the function type. If we have parameters, make sure their names
3670
- // get into the resulting function type.
3671
- ParameterList *bodyParams = nullptr ;
3672
- auto importedType = Impl.importFunctionType (
3673
- dc, decl, {decl->param_begin (), decl->param_size ()},
3674
- decl->isVariadic (), isInSystemModule (dc), name, bodyParams);
3675
- if (!importedType)
3676
- return nullptr ;
3672
+ if (!decl->hasPrototype ()) {
3673
+ // FIXME: source location...
3674
+ Impl.SwiftContext .Diags .diagnose ({}, diag::swift_name_no_prototype);
3675
+ Impl.SwiftContext .Diags .diagnose ({}, diag::note_while_importing,
3676
+ decl->getName ());
3677
+ return nullptr ;
3678
+ }
3677
3679
3678
- auto resultTy = importedType.getType ();
3679
- auto loc = Impl.importSourceLoc (decl->getLocation ());
3680
+ // There is an inout 'self' when the parameter is a pointer to a
3681
+ // non-const instance of the type we're importing onto. Importing this
3682
+ // as a method means that the method should be treated as mutating in
3683
+ // this situation.
3684
+ if (selfIdx &&
3685
+ !dc->getDeclaredInterfaceType ()->hasReferenceSemantics ()) {
3686
+ auto selfParam = decl->getParamDecl (*selfIdx);
3687
+ auto selfParamTy = selfParam->getType ();
3688
+ if ((selfParamTy->isPointerType () ||
3689
+ selfParamTy->isReferenceType ()) &&
3690
+ !selfParamTy->getPointeeType ().isConstQualified ()) {
3691
+ selfIsInOut = true ;
3692
+
3693
+ // If there's a swift_newtype, check the levels of indirection: self
3694
+ // is only inout if this is a pointer to the typedef type (which
3695
+ // itself is a pointer).
3696
+ if (auto nominalTypeDecl = dc->getSelfNominalTypeDecl ()) {
3697
+ if (auto clangDCTy = dyn_cast_or_null<clang::TypedefNameDecl>(
3698
+ nominalTypeDecl->getClangDecl ()))
3699
+ if (getSwiftNewtypeAttr (clangDCTy, getVersion ()))
3700
+ if (clangDCTy->getUnderlyingType ().getCanonicalType () !=
3701
+ selfParamTy->getPointeeType ().getCanonicalType ())
3702
+ selfIsInOut = false ;
3703
+ }
3704
+ }
3705
+ }
3706
+
3707
+ bool allowNSUIntegerAsInt =
3708
+ Impl.shouldAllowNSUIntegerAsInt (isInSystemModule (dc), decl);
3709
+
3710
+ bodyParams =
3711
+ getNonSelfParamList (dc, decl, selfIdx, name.getArgumentNames (),
3712
+ allowNSUIntegerAsInt, !name);
3713
+
3714
+ importedType =
3715
+ Impl.importFunctionReturnType (dc, decl, allowNSUIntegerAsInt);
3716
+ } else {
3717
+ // Import the function type. If we have parameters, make sure their
3718
+ // names get into the resulting function type.
3719
+ importedType = Impl.importFunctionType (
3720
+ dc, decl, {decl->param_begin (), decl->param_size ()},
3721
+ decl->isVariadic (), isInSystemModule (dc), name, bodyParams);
3722
+
3723
+ if (auto *mdecl = dyn_cast<clang::CXXMethodDecl>(decl)) {
3724
+ if (!mdecl->isStatic ()) {
3725
+ selfIdx = 0 ;
3726
+ // Workaround until proper const support is handled: Force
3727
+ // everything to be mutating. This implicitly makes the parameter
3728
+ // indirect.
3729
+ selfIsInOut = true ;
3730
+ } else {
3731
+ selfIdx = None;
3732
+ }
3733
+ }
3734
+ }
3680
3735
3681
3736
if (name && name.isSimpleName ()) {
3682
3737
assert (importedName.hasCustomName () &&
@@ -3685,34 +3740,46 @@ namespace {
3685
3740
name = DeclName (Impl.SwiftContext , name.getBaseName (), bodyParams);
3686
3741
}
3687
3742
3743
+ if (!importedType)
3744
+ return nullptr ;
3745
+
3746
+ auto resultTy = importedType.getType ();
3747
+ auto loc = Impl.importSourceLoc (decl->getLocation ());
3748
+
3688
3749
// FIXME: Poor location info.
3689
3750
auto nameLoc = Impl.importSourceLoc (decl->getLocation ());
3690
- FuncDecl *result = createFuncOrAccessor (Impl.SwiftContext , loc,
3691
- accessorInfo, name, nameLoc,
3692
- bodyParams, resultTy,
3693
- /* throws*/ false ,
3694
- dc, decl);
3695
-
3696
- if (auto *mdecl = dyn_cast<clang::CXXMethodDecl>(decl)) {
3697
- if (!mdecl->isStatic ()) {
3698
- // Workaround until proper const support is handled: Force
3699
- // everything to be mutating. This implicitly makes the parameter
3700
- // indirect.
3751
+ result = createFuncOrAccessor (Impl.SwiftContext , loc, accessorInfo, name,
3752
+ nameLoc, bodyParams, resultTy,
3753
+ /* throws*/ false , dc, decl);
3754
+
3755
+ result->setGenericEnvironment (dc->getGenericEnvironmentOfContext ());
3756
+
3757
+ if (!dc->isModuleScopeContext ()) {
3758
+ if (selfIsInOut)
3701
3759
result->setSelfAccessKind (SelfAccessKind::Mutating);
3702
- // "self" is the first argument.
3703
- result->setSelfIndex (0 );
3760
+ else
3761
+ result->setSelfAccessKind (SelfAccessKind::NonMutating);
3762
+ if (selfIdx) {
3763
+ result->setSelfIndex (selfIdx.getValue ());
3704
3764
} else {
3705
3765
result->setStatic ();
3706
3766
result->setImportAsStaticMember ();
3707
3767
}
3708
3768
}
3709
- result->computeType ();
3710
- result->setValidationToChecked ();
3769
+
3711
3770
result->setIsObjC (false );
3712
3771
result->setIsDynamic (false );
3772
+ result->computeType ();
3773
+ result->setValidationToChecked ();
3774
+
3713
3775
Impl.recordImplicitUnwrapForDecl (result,
3714
3776
importedType.isImplicitlyUnwrapped ());
3715
3777
3778
+ if (dc->getSelfClassDecl ())
3779
+ // FIXME: only if the class itself is not marked final
3780
+ result->getAttrs ().add (new (Impl.SwiftContext )
3781
+ FinalAttr (/* IsImplicit=*/ true ));
3782
+
3716
3783
// Someday, maybe this will need to be 'open' for C++ virtual methods.
3717
3784
result->setAccess (AccessLevel::Public);
3718
3785
finishFuncDecl (decl, result);
@@ -5779,105 +5846,6 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
5779
5846
return result;
5780
5847
}
5781
5848
5782
- Decl *SwiftDeclConverter::importGlobalAsMethod (
5783
- const clang::FunctionDecl *decl,
5784
- DeclName name,
5785
- DeclContext *dc,
5786
- Optional<unsigned > selfIdx,
5787
- Optional<ImportedName> correctSwiftName,
5788
- Optional<AccessorInfo> accessorInfo) {
5789
- if (dc->getSelfProtocolDecl () && !selfIdx) {
5790
- // FIXME: source location...
5791
- Impl.SwiftContext .Diags .diagnose ({}, diag::swift_name_protocol_static,
5792
- /* isInit=*/ false );
5793
- Impl.SwiftContext .Diags .diagnose ({}, diag::note_while_importing,
5794
- decl->getName ());
5795
- return nullptr ;
5796
- }
5797
-
5798
- if (!decl->hasPrototype ()) {
5799
- // FIXME: source location...
5800
- Impl.SwiftContext .Diags .diagnose ({}, diag::swift_name_no_prototype);
5801
- Impl.SwiftContext .Diags .diagnose ({}, diag::note_while_importing,
5802
- decl->getName ());
5803
- return nullptr ;
5804
- }
5805
-
5806
- bool allowNSUIntegerAsInt =
5807
- Impl.shouldAllowNSUIntegerAsInt (isInSystemModule (dc), decl);
5808
-
5809
- auto &C = Impl.SwiftContext ;
5810
- // There is an inout 'self' when the parameter is a pointer to a non-const
5811
- // instance of the type we're importing onto. Importing this as a method means
5812
- // that the method should be treated as mutating in this situation.
5813
- bool selfIsInOut = false ;
5814
- if (selfIdx && !dc->getDeclaredInterfaceType ()->hasReferenceSemantics ()) {
5815
- auto selfParam = decl->getParamDecl (*selfIdx);
5816
- auto selfParamTy = selfParam->getType ();
5817
- if ((selfParamTy->isPointerType () || selfParamTy->isReferenceType ()) &&
5818
- !selfParamTy->getPointeeType ().isConstQualified ()) {
5819
- selfIsInOut = true ;
5820
-
5821
- // If there's a swift_newtype, check the levels of indirection: self is
5822
- // only inout if this is a pointer to the typedef type (which itself is a
5823
- // pointer).
5824
- if (auto nominalTypeDecl = dc->getSelfNominalTypeDecl ()) {
5825
- if (auto clangDCTy = dyn_cast_or_null<clang::TypedefNameDecl>(
5826
- nominalTypeDecl->getClangDecl ()))
5827
- if (getSwiftNewtypeAttr (clangDCTy, getVersion ()))
5828
- if (clangDCTy->getUnderlyingType ().getCanonicalType () !=
5829
- selfParamTy->getPointeeType ().getCanonicalType ())
5830
- selfIsInOut = false ;
5831
- }
5832
- }
5833
- }
5834
-
5835
- auto *bodyParams = getNonSelfParamList (
5836
- dc, decl, selfIdx, name.getArgumentNames (), allowNSUIntegerAsInt, !name);
5837
-
5838
- auto importedType =
5839
- Impl.importFunctionReturnType (dc, decl, allowNSUIntegerAsInt);
5840
- Type swiftResultTy = importedType.getType ();
5841
-
5842
- auto loc = Impl.importSourceLoc (decl->getLocation ());
5843
- auto nameLoc = Impl.importSourceLoc (decl->getLocation ());
5844
- auto result =
5845
- createFuncOrAccessor (C, loc, accessorInfo, name, nameLoc,
5846
- bodyParams, swiftResultTy,
5847
- /* throws*/ false , dc, decl);
5848
-
5849
- result->setGenericEnvironment (dc->getGenericEnvironmentOfContext ());
5850
-
5851
- result->setAccess (AccessLevel::Public);
5852
- if (selfIsInOut)
5853
- result->setSelfAccessKind (SelfAccessKind::Mutating);
5854
- else
5855
- result->setSelfAccessKind (SelfAccessKind::NonMutating);
5856
- if (selfIdx) {
5857
- result->setSelfIndex (selfIdx.getValue ());
5858
- } else {
5859
- result->setStatic ();
5860
- result->setImportAsStaticMember ();
5861
- }
5862
-
5863
- result->computeType ();
5864
- result->setValidationToChecked ();
5865
-
5866
- Impl.recordImplicitUnwrapForDecl (result,
5867
- importedType.isImplicitlyUnwrapped ());
5868
-
5869
- assert (selfIdx ? result->getSelfIndex () == *selfIdx
5870
- : result->isImportAsStaticMember ());
5871
-
5872
- if (dc->getSelfClassDecl ())
5873
- // FIXME: only if the class itself is not marked final
5874
- result->getAttrs ().add (new (C) FinalAttr (/* IsImplicit=*/ true ));
5875
-
5876
- finishFuncDecl (decl, result);
5877
- if (correctSwiftName)
5878
- markAsVariant (result, *correctSwiftName);
5879
- return result;
5880
- }
5881
5849
5882
5850
// / Create an implicit property given the imported name of one of
5883
5851
// / the accessors.
0 commit comments