-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[NameLookup] ASTOOScope ontology #24594
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
[NameLookup] ASTOOScope ontology #24594
Conversation
05a1fd9
to
b48c168
Compare
@swift-ci please smoke test os x platform |
3 similar comments
@swift-ci please smoke test os x platform |
@swift-ci please smoke test os x platform |
@swift-ci please smoke test os x platform |
@slavapestov @DougGregor Here's a PR with the scopes. This code definitely should be refactored before it lands. In particular, I'm not sure that the use of class templates worked that well because I could not get the method instantiations to work as I wanted them to. Also, it's possible that multiple inheritance could clean it up a lot. It does manage to find the right lookup decls and baseDC's for all of the .swift files in the test directory. I'm starting to work on the IndexOfFirstOuterResults: there's one test where it differs. Next, will be getting the dependency info to match, then seeing how many tests pass or fail with the new lookup code & also performance work. |
@swift-ci please smoke test os x platform |
At this point, the code seems to get the right answer for IndexOfFirstOuterResult, too. Next stop: the dependency info. |
ffaec97
to
1d41fb5
Compare
@swift-ci please smoke test |
9979bfd
to
c3da9c4
Compare
c3da9c4
to
40d811e
Compare
@swift-ci please smoke test os x platform |
This is a huge patch, are you going to split this up? |
40d811e
to
663760e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some drive by comments. This is a really huge patch, it is important that it is split up. There is also a bunch of formatting (i.e. indentation) errors that would be caught by git-clang-format.
include/swift/AST/ASTScope.h
Outdated
@@ -1,4 +1,5 @@ | |||
//===--- ASTScope.h - Swift AST Scope ---------------------------*- C++ -*-===// | |||
//===--- ASTScopeImpl.h - Swift AST Object-Oriented Scope -----------*- C++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is misformatted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tx. Fixed
include/swift/AST/ASTScope.h
Outdated
@@ -10,117 +11,75 @@ | |||
// | |||
//===----------------------------------------------------------------------===// | |||
// | |||
// This file defines the ASTScope class and related functionality, which | |||
// This file defines the ASTScopeImpl class ontology, which |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Top level file headers should have a \file and 3 slashes. See:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tx for the pointer. Fixed.
include/swift/AST/ASTScope.h
Outdated
//===----------------------------------------------------------------------===// | ||
#ifndef SWIFT_AST_AST_SCOPE_H | ||
#define SWIFT_AST_AST_SCOPE_H | ||
#ifndef SWIFT_AST_AST_OO_SCOPE_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the normal way that we write these guards: file path. I.e.: SWIFT_AST_ASTSCOPE_H.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tx. Slipped by when I did a global rename.
class TypeAliasScope; | ||
class DeferredNodes; | ||
|
||
#pragma mark the root ASTScopeImpl class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally do not have pragma marks in our source code. Can you remove this when you are done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the way they help my eyes parse the file. Also Xcode supports them. Is there a substitute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually looking at some other parts of the frontend, if no one else has issues with it, then I am cool with it (looks like @DougGregor is using it a bunch).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
I could switch to // MARK if that's better
include/swift/AST/ASTScope.h
Outdated
|
||
namespace ast_scope { | ||
class ASTScopeImpl; | ||
class GTXScope; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does GTX stand for? Can you expand this out? We prefer to avoid abbreviations unless it is something really commonly known (i.e. AST).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Doxygen comment for GTXScope says GenericType or Extension Scope. I know it's a new abbreviation to the code base -- I've seen NTD used a bunch -- and I generally agree with spelling things out. If I spell it out, we get:
GenericTypeOrExtensionScope
GenericTypeOrExtensionWherePortion
GenericTypeOrExtensionWholePortion.
Seems to be getting a bit long, but I'm sympathetic to the idea of spelling it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make sure that each class with GTX in its name includes a comment with an explanation of the abbreviation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please spell it out. How often are you going to read vs write this code (also since you use Xcode, I imagine the IDE will spell it out for you via autocomplete). For dinosaurs like me who do not use a new IDE, not having it spelled out makes life difficult.
I need to look at the other two ones. There may be a better name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK--spelled it out.
@swift-ci please clean smoke test os x platform |
@gottesmm Thanks for taking a look! As far as splitting it up, the one piece that I can figure out to split off is the extra source location info for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More feedback, some questions and some suggestions on stuff that I think could be split out of the patch.
include/swift/AST/Expr.h
Outdated
@@ -4666,16 +4664,19 @@ class UnresolvedPatternExpr : public Expr { | |||
class EditorPlaceholderExpr : public Expr { | |||
Identifier Placeholder; | |||
SourceLoc Loc; | |||
SourceLoc TrailingAngleBracketLoc; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example of something that could be ripped out into a separate patch. This could be sliced off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, agreed! Our comments crossed in the ether.
class ASTScopeImpl; | ||
} // namespace ast_scope | ||
|
||
/// LookupResultEntry - One result of unqualified lookup. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you indenting this out, that just increases the diff without need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably a git-clang-format thing--I'll fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fixed. Let me know if not. I have tried to git-format it all but may have missed something.
@@ -110,13 +150,13 @@ class UnqualifiedLookup { | |||
/// is used to determine which declarations in that body are visible. | |||
UnqualifiedLookup(DeclName Name, DeclContext *DC, LazyResolver *TypeResolver, | |||
SourceLoc Loc = SourceLoc(), Options options = Options()); | |||
|
|||
SmallVector<LookupResultEntry, 4> Results; | |||
using ResultsVector = SmallVector<LookupResultEntry, 4>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you using this type outside of the class? If not, why create this? If you are using it outside, it is better to use SmallVectorImpl.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, UnqualifedLookupFactory
uses it, and no, the Impl type won't do. But is there a better way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wont it do. If you are passing it to someone, you can use SmallVectorImpl< LookupResultEntry> &
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In unqualifiedLookup.cpp
:
if (compareToASTScopes && useASTScopesForExperimentalLookupIfEnabled()) {
ResultsVector results;
size_t indexOfFirstOuterResult = 0;
UnqualifiedLookupFactory scopeLookup(Name, DC, TypeResolver, Loc, options,
results, indexOfFirstOuterResult);
The code needs to create a results vector, so it can make another factory for comparison purposes. Publishing the type seemed like the clearest way. I suppose the code could use a decltype
instead but it's not clearer.
|
||
SmallVector<LookupResultEntry, 4> Results; | ||
using ResultsVector = SmallVector<LookupResultEntry, 4>; | ||
ResultsVector Results; | ||
/// The index of the first result that isn't from the innermost scope |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put a newline line here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, good catch, thanks!
NominalTypeDecl *const nominal, | ||
Optional<bool> isCascadingUse) = 0; | ||
|
||
virtual void stopForDebuggingIfTargetLookup() = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this going to be called in the code always? If this is only for debugging the compiler, it should be hidden behind an #ifdef
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! Will do.
lib/AST/ASTScopeCreation.cpp
Outdated
if (se) | ||
ScopeCreator().visit(se, this, deferred); | ||
} | ||
void ASTScopeImpl::dispatchAndCreateIfNeeded(Decl *d, DeferredNodes &deferred) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make sure to put spaces in between things like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, a space there is good, thanks. Other places, I'm not so sure:
ASTScopeImpl *
ExtensionScope::createTrailingWhereClauseScope(ASTScopeImpl *parent) {
return parent->createSubtree2D<ExtensionScope, GTXWherePortion>(decl);
}
ASTScopeImpl *
NominalTypeScope::createTrailingWhereClauseScope(ASTScopeImpl *parent) {
return parent->createSubtree2D<NominalTypeScope, GTXWherePortion>(decl);
}
ASTScopeImpl *
TypeAliasScope::createTrailingWhereClauseScope(ASTScopeImpl *parent) {
return parent->createSubtree2D<TypeAliasScope, GTXWherePortion>(decl);
}
Here, I could use a define
, but it seems not quite an improvement for such a small number. So, I go with tight repetition.
lib/AST/ASTScopeCreation.cpp
Outdated
// If there's another conditional clause, add it as the child. | ||
if (index + 1 < getContainingStatement()->getCond().size()) | ||
createSubtreeForNextConditionalClause(deferred); | ||
else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everywhere else you did if () { } else { }. Can you be consistent with that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I see where I used braces but should not have. Thanks. I'm following a guideline from @jordan, which I quite like:
If the statement in the if
(etc) is visually short (typ. one line) then braces just add clutter.
But if the statement wraps, then braces are better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll go through the code and eliminate bogus braces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All I want is for you to be consistent about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite right and fair!
lib/AST/ASTScopeCreation.cpp
Outdated
createSubtree<ClosureBodyScope>(captureList, closureExpr); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another inconsistency around newlines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, fixed that one. But there will be places where I think that an extra newline can increase readability, just not there! Thanks.
lib/AST/ASTScopeCreation.cpp
Outdated
|
||
public: | ||
ClosureFinder( | ||
llvm::function_ref<void(NullablePtr<CaptureListExpr>, ClosureExpr *)> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a friendly FYI, I injected function_ref into the swift namespace a bit ago. You can drop the llvm::.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! I'll fix all my uses.
lib/AST/ASTScopeCreation.cpp
Outdated
: foundClosure(foundClosure) {} | ||
|
||
std::pair<bool, Expr *> walkToExprPre(Expr *E) override { | ||
if (auto closure = dyn_cast<ClosureExpr>(E)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put a * on the pointer auto to make it clear it is a pointer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks!
@swift-ci please clean smoke test os x platform |
@swift-ci please smoke test os x platform |
@swift-ci please smoke test |
@swift-ci please smoke test Linux platform |
Merged per offline conversation with @slavapestov |
This breaks on Windows! https://compnerd.visualstudio.com/windows-swift/_build/results?buildId=3205 |
…tree This is swift-5.1-branch only fix. The fix on master has been superseded by this pull request: swiftlang#24594 rdar://52128292
Initial cut at an ontology for scopes.
Disabled by default, but you can pass
-enable-astscope-lookup
to turn it on, or set an environment variable:export SWIFT_TEST_OPTIONS=-enable-astscope-lookup
to telllit
to pass that option. There's also-Xfrontend -compare-to-astscope-lookup
which will fail an assertion if the new lookup varies from the old.Parser-based lookup is still used for now, even with the new lookup.