Skip to content

Commit 1c24b88

Browse files
authored
Merge pull request swiftlang#70467 from AnthonyLatsis/recursive-member-typerepr
AST: Remodel `MemberTypeRepr` to be recursive
2 parents 63e42a4 + 8970afc commit 1c24b88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1779
-902
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,11 +1589,11 @@ BridgedInverseTypeRepr_createParsed(BridgedASTContext cContext,
15891589
BridgedSourceLoc cTildeLoc,
15901590
BridgedTypeRepr cConstraint);
15911591

1592-
SWIFT_NAME("BridgedMemberTypeRepr.createParsed(_:base:members:)")
1593-
BridgedTypeRepr
1594-
BridgedMemberTypeRepr_createParsed(BridgedASTContext cContext,
1595-
BridgedTypeRepr baseComponent,
1596-
BridgedArrayRef bridgedMemberComponents);
1592+
SWIFT_NAME("BridgedDeclRefTypeRepr.createParsed(_:base:name:nameLoc:genericArguments:angleRange:)")
1593+
BridgedDeclRefTypeRepr BridgedDeclRefTypeRepr_createParsed(
1594+
BridgedASTContext cContext, BridgedTypeRepr cBase, BridgedIdentifier cName,
1595+
BridgedSourceLoc cLoc, BridgedArrayRef cGenericArguments,
1596+
BridgedSourceRange cAngleRange);
15971597

15981598
SWIFT_NAME("BridgedMetatypeTypeRepr.createParsed(_:base:typeKeywordLoc:)")
15991599
BridgedMetatypeTypeRepr

include/swift/AST/ASTWalker.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,37 @@ enum class MacroWalking {
103103
None
104104
};
105105

106+
/// A scheme for walking a `MemberTypeRepr`.
107+
enum class MemberTypeReprWalkingScheme {
108+
/// Walk in source order, such that each subsequent dot-separated component is
109+
/// a child of the previous one. For example, walk `A.B<T.U>.C` like so
110+
/// (top-down order):
111+
///
112+
/// ```
113+
/// A
114+
/// ╰─B
115+
/// ├─T
116+
/// │ ╰─U
117+
/// ╰─C
118+
/// ```
119+
SourceOrderRecursive,
120+
121+
/// Walk in AST order (that is, according to how member type
122+
/// representations are modeled in the AST, such that each previous
123+
/// dot-separated component is a child of the subsequent one), base before
124+
/// generic arguments. For example, walk `A.B<T.U>.C` like so
125+
/// (top-down order):
126+
///
127+
/// ```
128+
/// C
129+
/// ╰─B
130+
/// ├─A
131+
/// ╰─U
132+
/// ╰─T
133+
/// ```
134+
ASTOrderRecursive
135+
};
136+
106137
/// An abstract class used to traverse an AST.
107138
class ASTWalker {
108139
public:
@@ -563,6 +594,11 @@ class ASTWalker {
563594
return Action::Continue();
564595
}
565596

597+
/// This method configures how to walk `MemberTypeRepr` nodes.
598+
virtual MemberTypeReprWalkingScheme getMemberTypeReprWalkingScheme() const {
599+
return MemberTypeReprWalkingScheme::ASTOrderRecursive;
600+
}
601+
566602
/// This method configures whether the walker should explore into the generic
567603
/// params in AbstractFunctionDecl and NominalTypeDecl.
568604
virtual bool shouldWalkIntoGenericParams() { return false; }

include/swift/AST/Attr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ASTContext;
5454
struct PrintOptions;
5555
class CustomAttr;
5656
class Decl;
57+
class DeclRefTypeRepr;
5758
class AbstractFunctionDecl;
5859
class FuncDecl;
5960
class ClassDecl;
@@ -1766,7 +1767,7 @@ class CustomAttr final : public DeclAttribute {
17661767
///
17671768
/// For an identifier type repr, return a pair of `nullptr` and the
17681769
/// identifier.
1769-
std::pair<IdentTypeRepr *, IdentTypeRepr *> destructureMacroRef();
1770+
std::pair<IdentTypeRepr *, DeclRefTypeRepr *> destructureMacroRef();
17701771

17711772
/// Whether the attribute has any arguments.
17721773
bool hasArgs() const { return argList != nullptr; }

include/swift/AST/TypeDeclFinder.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace swift {
2020

2121
class BoundGenericType;
22-
class IdentTypeRepr;
22+
class DeclRefTypeRepr;
2323
class NominalType;
2424
class TypeAliasType;
2525

@@ -57,15 +57,13 @@ class SimpleTypeDeclFinder : public TypeDeclFinder {
5757
: Callback(callback) {}
5858
};
5959

60-
/// Walks a \c TypeRepr to find all \c IdentTypeRepr nodes with bound
61-
/// type declarations.
62-
///
63-
/// Subclasses can either override #visitTypeDecl if they only care about
64-
/// types on their own, or #visitIdentTypeRepr if they want to keep
65-
/// the TypeRepr around.
66-
class TypeReprIdentFinder : public ASTWalker {
67-
/// The function to call when a \c IdentTypeRepr is seen.
68-
llvm::function_ref<bool(const IdentTypeRepr *)> Callback;
60+
/// Walks a `TypeRepr` and reports all `DeclRefTypeRepr` nodes with bound
61+
/// type declarations by invoking a given callback. These nodes are reported in
62+
/// depth- and base-first AST order. For example, nodes in `A<T>.B<U>` will be
63+
/// reported in the following order: `TAUB`.
64+
class DeclRefTypeReprFinder : public ASTWalker {
65+
/// The function to call when a `DeclRefTypeRepr` is seen.
66+
llvm::function_ref<bool(const DeclRefTypeRepr *)> Callback;
6967

7068
MacroWalking getMacroWalkingBehavior() const override {
7169
return MacroWalking::Arguments;
@@ -74,11 +72,10 @@ class TypeReprIdentFinder : public ASTWalker {
7472
PostWalkAction walkToTypeReprPost(TypeRepr *TR) override;
7573

7674
public:
77-
explicit TypeReprIdentFinder(
78-
llvm::function_ref<bool(const IdentTypeRepr *)> callback)
79-
: Callback(callback) {}
75+
explicit DeclRefTypeReprFinder(
76+
llvm::function_ref<bool(const DeclRefTypeRepr *)> callback)
77+
: Callback(callback) {}
8078
};
81-
8279
}
8380

8481
#endif

0 commit comments

Comments
 (0)