Skip to content

Commit c9576ca

Browse files
committed
Move LegacyUnqualifiedLookup out of header
1 parent 306cfd8 commit c9576ca

File tree

2 files changed

+128
-136
lines changed

2 files changed

+128
-136
lines changed

include/swift/AST/NameLookup.h

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ struct LookupResultEntry {
8484
ValueDecl *getBaseDecl() const;
8585
};
8686

87-
class LegacyUnqualifiedLookup; // for verifyEqual below
88-
8987
/// This class implements and represents the result of performing
9088
/// unqualified lookup (i.e. lookup for a plain identifier).
9189
class UnqualifiedLookup {
@@ -131,100 +129,13 @@ class UnqualifiedLookup {
131129

132130
/// Get the result as a single type, or a null type if that fails.
133131
TypeDecl *getSingleTypeResult() const;
134-
135-
bool verifyEqual(const LegacyUnqualifiedLookup &&) const;
136-
137-
public: // for exp debugging
138-
SourceFile const *recordedSF = nullptr;
139-
DeclName recordedName;
140-
bool recordedIsCascadingUse = false;
141-
142-
// TODO: move into Factory
143-
struct PlacesToSearch {
144-
// TODO: constify members?
145-
/// The context in which the places where found.
146-
DeclContext *fromWhence;
147-
/// Nontypes are formally members of the base type
148-
DeclContext *whereNonTypesAreMembers;
149-
/// Types are formally members of the metatype
150-
DeclContext *whereTypesAreMembers;
151-
/// Places to search for the lookup.
152-
SmallVector<NominalTypeDecl *, 2> places;
153-
154-
PlacesToSearch(DeclContext *fromWhence,
155-
DeclContext *whereNonTypesAreMembers,
156-
DeclContext *whereTypesAreMembers,
157-
DeclContext *placesHolder);
158-
bool empty() const {
159-
return whereNonTypesAreMembers == nullptr || places.empty();
160-
}
161-
// Classify this declaration.
162-
// Types are formally members of the metatype.
163-
DeclContext *whereValueIsMember(const ValueDecl *const member) const {
164-
return dyn_cast<TypeDecl>(member) ? whereTypesAreMembers
165-
: whereNonTypesAreMembers;
166-
}
167-
void addToResults(const DeclName &Name, bool isCascadingUse,
168-
NLOptions baseNLOptions, DeclContext *contextForLookup,
169-
SmallVectorImpl<LookupResultEntry> &results) const;
170-
void dump() const;
171-
};
172-
173-
struct PerScopeLookupState {
174-
bool isDone;
175-
DeclContext *childOfNextDC;
176-
Optional<PlacesToSearch> placesToSearch;
177-
Optional<bool> isCascadingUse;
178-
179-
void dump() const;
180-
};
181-
std::vector<PerScopeLookupState> breadcrumbs;
182-
void dumpBreadcrumbs() const;
183132
};
184133

185134
inline UnqualifiedLookup::Options operator|(UnqualifiedLookup::Flags flag1,
186135
UnqualifiedLookup::Flags flag2) {
187136
return UnqualifiedLookup::Options(flag1) | flag2;
188137
}
189138

190-
/// This class implements and represents the result of performing
191-
/// unqualified lookup (i.e. lookup for a plain identifier).
192-
/// It is being kept around in order to check the refactoring against the new
193-
/// UnqualifiedLookup above.
194-
class LegacyUnqualifiedLookup {
195-
public:
196-
using Flags = UnqualifiedLookup::Flags;
197-
using Options = UnqualifiedLookup::Options;
198-
199-
/// Lookup an unqualified identifier \p Name in the context.
200-
///
201-
/// If the current DeclContext is nested in a function body, the SourceLoc
202-
/// is used to determine which declarations in that body are visible.
203-
LegacyUnqualifiedLookup(DeclName Name, DeclContext *DC,
204-
LazyResolver *TypeResolver,
205-
SourceLoc Loc = SourceLoc(),
206-
Options options = Options());
207-
208-
SmallVector<LookupResultEntry, 4> Results;
209-
/// The index of the first result that isn't from the innermost scope
210-
/// with results.
211-
///
212-
/// That is, \c makeArrayRef(Results).take_front(IndexOfFirstOuterResults)
213-
/// will be Results from the innermost scope that had results, and the
214-
/// remaining elements of Results will be from parent scopes of this one.
215-
size_t IndexOfFirstOuterResult;
216-
217-
/// Return true if anything was found by the name lookup.
218-
bool isSuccess() const { return !Results.empty(); }
219-
220-
/// Get the result as a single type, or a null type if that fails.
221-
TypeDecl *getSingleTypeResult() const;
222-
223-
public: // for exp debugging
224-
SourceFile const *recordedSF = nullptr;
225-
DeclName recordedName;
226-
bool recordedIsCascadingUse = false;
227-
};
228139

229140
/// Describes the reason why a certain declaration is visible.
230141
enum class DeclVisibilityKind {

lib/AST/UnqualifiedLookup.cpp

Lines changed: 128 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,53 @@ template <typename D> bool shouldLookupMembers(D *decl, SourceLoc loc) {
136136
} // end anonymous namespace
137137

138138
namespace {
139+
class LegacyUnqualifiedLookup;
140+
139141
class UnqualifiedLookupFactory {
140142
public:
141-
using Flags = LegacyUnqualifiedLookup::Flags;
142-
using Options = LegacyUnqualifiedLookup::Options;
143-
using PlacesToSearch = UnqualifiedLookup::PlacesToSearch;
144-
using PerScopeLookupState = UnqualifiedLookup::PerScopeLookupState;
143+
using Flags = UnqualifiedLookup::Flags;
144+
using Options = UnqualifiedLookup::Options;
145145

146146
private:
147+
struct PlacesToSearch {
148+
// TODO: constify members?
149+
/// The context in which the places where found.
150+
DeclContext *fromWhence;
151+
/// Nontypes are formally members of the base type
152+
DeclContext *whereNonTypesAreMembers;
153+
/// Types are formally members of the metatype
154+
DeclContext *whereTypesAreMembers;
155+
/// Places to search for the lookup.
156+
SmallVector<NominalTypeDecl *, 2> places;
157+
158+
PlacesToSearch(DeclContext *fromWhence,
159+
DeclContext *whereNonTypesAreMembers,
160+
DeclContext *whereTypesAreMembers,
161+
DeclContext *placesHolder);
162+
bool empty() const {
163+
return whereNonTypesAreMembers == nullptr || places.empty();
164+
}
165+
// Classify this declaration.
166+
// Types are formally members of the metatype.
167+
DeclContext *whereValueIsMember(const ValueDecl *const member) const {
168+
return dyn_cast<TypeDecl>(member) ? whereTypesAreMembers
169+
: whereNonTypesAreMembers;
170+
}
171+
void addToResults(const DeclName &Name, bool isCascadingUse,
172+
NLOptions baseNLOptions, DeclContext *contextForLookup,
173+
SmallVectorImpl<LookupResultEntry> &results) const;
174+
void dump() const;
175+
};
176+
177+
struct PerScopeLookupState {
178+
bool isDone;
179+
DeclContext *childOfNextDC;
180+
Optional<PlacesToSearch> placesToSearch;
181+
Optional<bool> isCascadingUse;
182+
183+
void dump() const;
184+
};
185+
147186
// Inputs
148187
const DeclName Name;
149188
DeclContext *const DC;
@@ -162,15 +201,14 @@ class UnqualifiedLookupFactory {
162201
// Outputs
163202
SmallVectorImpl<LookupResultEntry> &Results;
164203
size_t &IndexOfFirstOuterResult;
165-
166-
// For debugging:
167-
SourceFile const *&recordedSF;
168-
DeclName &recordedName;
169-
bool &recordedIsCascadingUse;
170-
std::vector<PerScopeLookupState> &breadcrumbs;
171-
172204
SmallVector<LookupResultEntry, 4> UnavailableInnerResults;
173205

206+
public: // for exp debugging
207+
std::vector<PerScopeLookupState> breadcrumbs;
208+
SourceFile const *recordedSF = nullptr;
209+
DeclName recordedName;
210+
bool recordedIsCascadingUse = false;
211+
174212
public:
175213
// clang-format off
176214
UnqualifiedLookupFactory(DeclName Name,
@@ -311,12 +349,17 @@ class UnqualifiedLookupFactory {
311349

312350
#pragma mark common helper declarations
313351
static NLOptions
314-
computeBaseNLOptions(const LegacyUnqualifiedLookup::Options options,
352+
computeBaseNLOptions(const UnqualifiedLookup::Options options,
315353
const bool isOriginallyTypeLookup);
316354

317355
static bool resolveIsCascadingUse(const DeclContext *const dc,
318356
Optional<bool> isCascadingUse,
319357
bool onlyCareAboutFunctionBody);
358+
359+
void dumpBreadcrumbs() const;
360+
361+
public:
362+
bool verifyEqualToLegacy(const LegacyUnqualifiedLookup &&LUL) const;
320363
};
321364
} // namespace
322365

@@ -344,11 +387,8 @@ isOriginallyTypeLookup(options.contains(Flags::TypeLookup)),
344387
baseNLOptions(computeBaseNLOptions(options, isOriginallyTypeLookup)),
345388
Consumer(Name, lookupToBeCreated.Results, isOriginallyTypeLookup),
346389
Results(lookupToBeCreated.Results),
347-
IndexOfFirstOuterResult(lookupToBeCreated.IndexOfFirstOuterResult),
348-
recordedSF(lookupToBeCreated.recordedSF),
349-
recordedName(lookupToBeCreated.recordedName),
350-
recordedIsCascadingUse(lookupToBeCreated.recordedIsCascadingUse),
351-
breadcrumbs(lookupToBeCreated.breadcrumbs) {}
390+
IndexOfFirstOuterResult(lookupToBeCreated.IndexOfFirstOuterResult)
391+
{}
352392
// clang-format on
353393

354394
void UnqualifiedLookupFactory::fillInLookup() {
@@ -948,7 +988,7 @@ bool UnqualifiedLookupFactory::addLocalVariableResults(DeclContext *dc) {
948988
return false;
949989
}
950990

951-
void UnqualifiedLookup::PlacesToSearch::addToResults(
991+
void UnqualifiedLookupFactory::PlacesToSearch::addToResults(
952992
const DeclName &Name, bool isCascadingUse, NLOptions baseNLOptions,
953993
DeclContext *contextForLookup,
954994
SmallVectorImpl<LookupResultEntry> &results) const {
@@ -1102,6 +1142,47 @@ bool UnqualifiedLookupFactory::resolveIsCascadingUse(
11021142
/*functionsAreNonCascading=*/onlyCareAboutFunctionBody);
11031143
}
11041144

1145+
namespace {
1146+
/// This class implements and represents the result of performing
1147+
/// unqualified lookup (i.e. lookup for a plain identifier).
1148+
/// It is being kept around in order to check the refactoring against the new
1149+
/// UnqualifiedLookup above.
1150+
class LegacyUnqualifiedLookup {
1151+
public:
1152+
using Flags = UnqualifiedLookup::Flags;
1153+
using Options = UnqualifiedLookup::Options;
1154+
1155+
/// Lookup an unqualified identifier \p Name in the context.
1156+
///
1157+
/// If the current DeclContext is nested in a function body, the SourceLoc
1158+
/// is used to determine which declarations in that body are visible.
1159+
LegacyUnqualifiedLookup(DeclName Name, DeclContext *DC,
1160+
LazyResolver *TypeResolver,
1161+
SourceLoc Loc = SourceLoc(),
1162+
Options options = Options());
1163+
1164+
SmallVector<LookupResultEntry, 4> Results;
1165+
/// The index of the first result that isn't from the innermost scope
1166+
/// with results.
1167+
///
1168+
/// That is, \c makeArrayRef(Results).take_front(IndexOfFirstOuterResults)
1169+
/// will be Results from the innermost scope that had results, and the
1170+
/// remaining elements of Results will be from parent scopes of this one.
1171+
size_t IndexOfFirstOuterResult;
1172+
1173+
/// Return true if anything was found by the name lookup.
1174+
bool isSuccess() const { return !Results.empty(); }
1175+
1176+
/// Get the result as a single type, or a null type if that fails.
1177+
TypeDecl *getSingleTypeResult() const;
1178+
1179+
public: // for exp debugging
1180+
SourceFile const *recordedSF = nullptr;
1181+
DeclName recordedName;
1182+
bool recordedIsCascadingUse = false;
1183+
};
1184+
}; // namespace
1185+
11051186
LegacyUnqualifiedLookup::LegacyUnqualifiedLookup(DeclName Name, DeclContext *DC,
11061187
LazyResolver *TypeResolver,
11071188
SourceLoc Loc, Options options)
@@ -1624,15 +1705,15 @@ TypeDecl *LegacyUnqualifiedLookup::getSingleTypeResult() const {
16241705
return dyn_cast<TypeDecl>(Results.back().getValueDecl());
16251706
}
16261707

1627-
UnqualifiedLookup::PlacesToSearch::PlacesToSearch(
1708+
UnqualifiedLookupFactory::PlacesToSearch::PlacesToSearch(
16281709
DeclContext *fromWhence, DeclContext *whereNonTypesAreMembers,
16291710
DeclContext *whereTypesAreMembers, DeclContext *placesHolder)
16301711
: fromWhence(fromWhence), whereNonTypesAreMembers(whereNonTypesAreMembers),
16311712
whereTypesAreMembers(whereTypesAreMembers) {
16321713
populateLookupDeclsFromContext(placesHolder, places);
16331714
}
16341715

1635-
void UnqualifiedLookup::PlacesToSearch::dump() const {
1716+
void UnqualifiedLookupFactory::PlacesToSearch::dump() const {
16361717
llvm::errs() << "fromWhence: ";
16371718
fromWhence->dumpContext();
16381719
llvm::errs() << "whereNonTypesAreMembers: ";
@@ -1655,9 +1736,9 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name,
16551736
Options options)
16561737
// clang-format on
16571738
: IndexOfFirstOuterResult(0) {
1658-
UnqualifiedLookupFactory(Name, DC, TypeResolver, Loc, options, *this)
1659-
.fillInLookup();
1660-
assert(verifyEqual(
1739+
UnqualifiedLookupFactory factory(Name, DC, TypeResolver, Loc, options, *this);
1740+
factory.fillInLookup();
1741+
assert(factory.verifyEqualToLegacy(
16611742
LegacyUnqualifiedLookup(Name, DC, TypeResolver, Loc, options)) &&
16621743
"bad refactoring");
16631744
}
@@ -1668,29 +1749,7 @@ TypeDecl *UnqualifiedLookup::getSingleTypeResult() const {
16681749
return dyn_cast<TypeDecl>(Results.back().getValueDecl());
16691750
}
16701751

1671-
bool UnqualifiedLookup::verifyEqual(
1672-
const LegacyUnqualifiedLookup &&other) const {
1673-
assert(Results.size() == other.Results.size());
1674-
for (size_t i : indices(Results)) {
1675-
const auto &e = Results[i];
1676-
const auto &oe = other.Results[i];
1677-
assert(e.getValueDecl() == oe.getValueDecl());
1678-
assert(e.getDeclContext() == oe.getDeclContext());
1679-
// unsigned printContext(llvm::raw_ostream &OS, unsigned indent = 0,
1680-
// bool onlyAPartialLine = false) const;
1681-
assert(e.getBaseDecl() == oe.getBaseDecl());
1682-
}
1683-
assert(IndexOfFirstOuterResult == other.IndexOfFirstOuterResult);
1684-
assert(recordedSF == other.recordedSF);
1685-
assert(recordedName == other.recordedName);
1686-
if (recordedIsCascadingUse)
1687-
assert(other.recordedIsCascadingUse);
1688-
else
1689-
assert(!other.recordedIsCascadingUse);
1690-
return true;
1691-
}
1692-
1693-
void UnqualifiedLookup::dumpBreadcrumbs() const {
1752+
void UnqualifiedLookupFactory::dumpBreadcrumbs() const {
16941753
auto &e = llvm::errs();
16951754
for (size_t i : indices(breadcrumbs)) {
16961755
e << i << "\n";
@@ -1699,11 +1758,33 @@ void UnqualifiedLookup::dumpBreadcrumbs() const {
16991758
}
17001759
}
17011760

1702-
void UnqualifiedLookup::PerScopeLookupState::dump() const {
1761+
void UnqualifiedLookupFactory::PerScopeLookupState::dump() const {
17031762
auto &e = llvm::errs();
17041763
e << (isDone ? "done: " : "not done: ");
17051764
e << " dc: ";
17061765
childOfNextDC->dumpContext();
17071766
if (placesToSearch.hasValue())
17081767
placesToSearch.getValue().dump();
17091768
}
1769+
1770+
bool UnqualifiedLookupFactory::verifyEqualToLegacy(
1771+
const LegacyUnqualifiedLookup &&LUL) const {
1772+
assert(Results.size() == LUL.Results.size());
1773+
for (size_t i : indices(Results)) {
1774+
const auto &e = Results[i];
1775+
const auto &oe = LUL.Results[i];
1776+
assert(e.getValueDecl() == oe.getValueDecl());
1777+
assert(e.getDeclContext() == oe.getDeclContext());
1778+
// unsigned printContext(llvm::raw_ostream &OS, unsigned indent = 0,
1779+
// bool onlyAPartialLine = false) const;
1780+
assert(e.getBaseDecl() == oe.getBaseDecl());
1781+
}
1782+
assert(IndexOfFirstOuterResult == LUL.IndexOfFirstOuterResult);
1783+
assert(recordedSF == LUL.recordedSF);
1784+
assert(recordedName == LUL.recordedName);
1785+
if (recordedIsCascadingUse)
1786+
assert(LUL.recordedIsCascadingUse);
1787+
else
1788+
assert(!LUL.recordedIsCascadingUse);
1789+
return true;
1790+
}

0 commit comments

Comments
 (0)