Skip to content

Commit 778f0f8

Browse files
authored
Separate TypeDeclFinder from AccessScopeChecker (#23957)
...and move the latter into lib/Sema as TypeAccessScopeChecker, since it's not interesting outside of Sema anyway. No functionality change.
1 parent 99341a7 commit 778f0f8

File tree

8 files changed

+150
-140
lines changed

8 files changed

+150
-140
lines changed

include/swift/AST/AccessScopeChecker.h renamed to include/swift/AST/TypeDeclFinder.h

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,28 @@
1-
//===--- AccessScopeChecker.h - Access calculation helpers -----*- C++ -*-===//
1+
//===--- TypeDeclFinder.h - Finds TypeDecls in Types/TypeReprs --*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
//
13-
// This file defines helpers for access-control calculation.
14-
//
15-
//===----------------------------------------------------------------------===//
16-
#ifndef SWIFT_ACCESS_SCOPE_CHECKER_H
17-
#define SWIFT_ACCESS_SCOPE_CHECKER_H
12+
#ifndef SWIFT_AST_TYPEDECLFINDER_H
13+
#define SWIFT_AST_TYPEDECLFINDER_H
1814

19-
#include "swift/AST/AccessScope.h"
20-
#include "swift/AST/AttrKind.h"
2115
#include "swift/AST/ASTWalker.h"
2216
#include "swift/AST/TypeWalker.h"
17+
#include "llvm/ADT/STLExtras.h"
2318

2419
namespace swift {
2520

2621
class BoundGenericType;
2722
class ComponentIdentTypeRepr;
2823
class NominalType;
29-
class SourceFile;
3024
class TypeAliasType;
3125

32-
class AccessScopeChecker {
33-
const SourceFile *File;
34-
bool TreatUsableFromInlineAsPublic;
35-
36-
Optional<AccessScope> Scope = AccessScope::getPublic();
37-
38-
AccessScopeChecker(const DeclContext *useDC,
39-
bool treatUsableFromInlineAsPublic);
40-
bool visitDecl(const ValueDecl *VD);
41-
42-
public:
43-
static Optional<AccessScope>
44-
getAccessScope(TypeRepr *TR, const DeclContext *useDC,
45-
bool treatUsableFromInlineAsPublic = false);
46-
static Optional<AccessScope>
47-
getAccessScope(Type T, const DeclContext *useDC,
48-
bool treatUsableFromInlineAsPublic = false);
49-
};
50-
5126
/// Walks a Type to find all NominalTypes, BoundGenericTypes, and
5227
/// TypeAliasTypes.
5328
class TypeDeclFinder : public TypeWalker {

lib/AST/AccessScopeChecker.cpp

Lines changed: 0 additions & 99 deletions
This file was deleted.

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ else()
1414
endif()
1515

1616
add_swift_host_library(swiftAST STATIC
17-
AccessScopeChecker.cpp
1817
AccessRequests.cpp
1918
ASTContext.cpp
2019
ASTDemangler.cpp
@@ -68,6 +67,7 @@ add_swift_host_library(swiftAST STATIC
6867
SwiftNameTranslation.cpp
6968
Type.cpp
7069
TypeCheckRequests.cpp
70+
TypeDeclFinder.cpp
7171
TypeJoinMeet.cpp
7272
TypeRefinementContext.cpp
7373
TypeRepr.cpp

lib/AST/TypeDeclFinder.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===--- TypeDeclFinder.cpp - Finds TypeDecls inside Types/TypeReprs ------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/TypeDeclFinder.h"
14+
#include "swift/AST/Decl.h"
15+
#include "swift/AST/TypeRepr.h"
16+
#include "swift/AST/Types.h"
17+
18+
using namespace swift;
19+
20+
TypeWalker::Action TypeDeclFinder::walkToTypePre(Type T) {
21+
if (auto *TAT = dyn_cast<TypeAliasType>(T.getPointer()))
22+
return visitTypeAliasType(TAT);
23+
24+
// FIXME: We're looking through sugar here so that we visit, e.g.,
25+
// Swift.Array when we see `[Int]`. But that means we do redundant work when
26+
// we see sugar that's purely structural, like `(Int)`. Fortunately, paren
27+
// types are the only such purely structural sugar at the time this comment
28+
// was written, and they're not so common in the first place.
29+
if (auto *BGT = T->getAs<BoundGenericType>())
30+
return visitBoundGenericType(BGT);
31+
if (auto *NT = T->getAs<NominalType>())
32+
return visitNominalType(NT);
33+
34+
return Action::Continue;
35+
}
36+
37+
TypeWalker::Action
38+
SimpleTypeDeclFinder::visitNominalType(NominalType *ty) {
39+
return Callback(ty->getDecl());
40+
}
41+
42+
TypeWalker::Action
43+
SimpleTypeDeclFinder::visitBoundGenericType(BoundGenericType *ty) {
44+
return Callback(ty->getDecl());
45+
}
46+
47+
TypeWalker::Action
48+
SimpleTypeDeclFinder::visitTypeAliasType(TypeAliasType *ty) {
49+
return Callback(ty->getDecl());
50+
}
51+
52+
bool TypeReprIdentFinder::walkToTypeReprPost(TypeRepr *TR) {
53+
auto CITR = dyn_cast<ComponentIdentTypeRepr>(TR);
54+
if (!CITR || !CITR->getBoundDecl())
55+
return true;
56+
return Callback(CITR);
57+
}

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
#include "TypeChecker.h"
1818
#include "TypeCheckAvailability.h"
19-
#include "swift/AST/AccessScopeChecker.h"
2019
#include "swift/AST/Attr.h"
2120
#include "swift/AST/Decl.h"
2221
#include "swift/AST/DeclContext.h"
2322
#include "swift/AST/Initializer.h"
2423
#include "swift/AST/ProtocolConformance.h"
24+
#include "swift/AST/TypeDeclFinder.h"
2525

2626
using namespace swift;
2727
using FragileFunctionKind = TypeChecker::FragileFunctionKind;

lib/Sema/TypeAccessScopeChecker.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===--- TypeAccessScopeChecker.h - Computes access for Types ---*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SEMA_TYPEACCESSSCOPECHECKER_H
14+
#define SWIFT_SEMA_TYPEACCESSSCOPECHECKER_H
15+
16+
#include "swift/AST/Decl.h"
17+
#include "swift/AST/DeclContext.h"
18+
#include "swift/AST/Type.h"
19+
#include "swift/AST/TypeDeclFinder.h"
20+
#include "swift/AST/TypeRepr.h"
21+
22+
namespace swift {
23+
class SourceFile;
24+
25+
/// Computes the access scope where a Type or TypeRepr is available, which is
26+
/// the intersection of all the scopes where the declarations referenced in the
27+
/// Type or TypeRepr are available.
28+
class TypeAccessScopeChecker {
29+
const SourceFile *File;
30+
bool TreatUsableFromInlineAsPublic;
31+
32+
Optional<AccessScope> Scope = AccessScope::getPublic();
33+
34+
TypeAccessScopeChecker(const DeclContext *useDC,
35+
bool treatUsableFromInlineAsPublic)
36+
: File(useDC->getParentSourceFile()),
37+
TreatUsableFromInlineAsPublic(treatUsableFromInlineAsPublic) {}
38+
39+
bool visitDecl(const ValueDecl *VD) {
40+
if (isa<GenericTypeParamDecl>(VD))
41+
return true;
42+
43+
auto AS = VD->getFormalAccessScope(File, TreatUsableFromInlineAsPublic);
44+
Scope = Scope->intersectWith(AS);
45+
return Scope.hasValue();
46+
}
47+
48+
public:
49+
static Optional<AccessScope>
50+
getAccessScope(TypeRepr *TR, const DeclContext *useDC,
51+
bool treatUsableFromInlineAsPublic = false) {
52+
TypeAccessScopeChecker checker(useDC, treatUsableFromInlineAsPublic);
53+
TR->walk(TypeReprIdentFinder([&](const ComponentIdentTypeRepr *typeRepr) {
54+
return checker.visitDecl(typeRepr->getBoundDecl());
55+
}));
56+
return checker.Scope;
57+
}
58+
59+
static Optional<AccessScope>
60+
getAccessScope(Type T, const DeclContext *useDC,
61+
bool treatUsableFromInlineAsPublic = false) {
62+
TypeAccessScopeChecker checker(useDC, treatUsableFromInlineAsPublic);
63+
T.walk(SimpleTypeDeclFinder([&](const ValueDecl *VD) {
64+
if (checker.visitDecl(VD))
65+
return TypeWalker::Action::Continue;
66+
return TypeWalker::Action::Stop;
67+
}));
68+
return checker.Scope;
69+
}
70+
};
71+
72+
} // end namespace swift
73+
74+
#endif

lib/Sema/TypeCheckAccess.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616

1717
#include "TypeChecker.h"
1818
#include "TypeCheckAccess.h"
19-
#include "swift/AST/AccessScopeChecker.h"
19+
#include "TypeAccessScopeChecker.h"
2020
#include "swift/AST/ASTVisitor.h"
2121
#include "swift/AST/ASTWalker.h"
2222
#include "swift/AST/ExistentialLayout.h"
2323
#include "swift/AST/Pattern.h"
2424
#include "swift/AST/ParameterList.h"
2525
#include "swift/AST/ProtocolConformance.h"
2626
#include "swift/AST/TypeCheckRequests.h"
27+
#include "swift/AST/TypeDeclFinder.h"
2728

2829
using namespace swift;
2930

@@ -204,7 +205,8 @@ void AccessControlCheckerBase::checkTypeAccessImpl(
204205
AccessScope problematicAccessScope = AccessScope::getPublic();
205206
if (type) {
206207
Optional<AccessScope> typeAccessScope =
207-
AccessScopeChecker::getAccessScope(type, useDC, checkUsableFromInline);
208+
TypeAccessScopeChecker::getAccessScope(type, useDC,
209+
checkUsableFromInline);
208210

209211
// Note: This means that the type itself is invalid for this particular
210212
// context, because it references declarations from two incompatible scopes.
@@ -225,8 +227,8 @@ void AccessControlCheckerBase::checkTypeAccessImpl(
225227
return;
226228

227229
Optional<AccessScope> typeReprAccessScope =
228-
AccessScopeChecker::getAccessScope(typeRepr, useDC,
229-
checkUsableFromInline);
230+
TypeAccessScopeChecker::getAccessScope(typeRepr, useDC,
231+
checkUsableFromInline);
230232
if (!typeReprAccessScope.hasValue())
231233
return;
232234

@@ -253,8 +255,8 @@ void AccessControlCheckerBase::checkTypeAccessImpl(
253255
//
254256
// Downgrade the error to a warning in this case for source compatibility.
255257
Optional<AccessScope> typeReprAccessScope =
256-
AccessScopeChecker::getAccessScope(typeRepr, useDC,
257-
checkUsableFromInline);
258+
TypeAccessScopeChecker::getAccessScope(typeRepr, useDC,
259+
checkUsableFromInline);
258260
assert(typeReprAccessScope && "valid Type but not valid TypeRepr?");
259261
if (contextAccessScope.hasEqualDeclContextWith(*typeReprAccessScope) ||
260262
contextAccessScope.isChildOf(*typeReprAccessScope)) {

0 commit comments

Comments
 (0)