Skip to content

[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

Merged
merged 116 commits into from
Jun 14, 2019

Conversation

davidungar
Copy link
Contributor

@davidungar davidungar commented May 8, 2019

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 tell lit 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.

@davidungar davidungar force-pushed the A5-7-ASTOOScope-rebased branch from 05a1fd9 to b48c168 Compare May 8, 2019 00:11
@davidungar
Copy link
Contributor Author

@swift-ci please smoke test os x platform

3 similar comments
@davidungar
Copy link
Contributor Author

@swift-ci please smoke test os x platform

@davidungar
Copy link
Contributor Author

@swift-ci please smoke test os x platform

@davidungar
Copy link
Contributor Author

@swift-ci please smoke test os x platform

@davidungar
Copy link
Contributor Author

@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.
Thanks!

@davidungar
Copy link
Contributor Author

@swift-ci please smoke test os x platform

@davidungar
Copy link
Contributor Author

At this point, the code seems to get the right answer for IndexOfFirstOuterResult, too. Next stop: the dependency info.

@davidungar davidungar force-pushed the A5-7-ASTOOScope-rebased branch from ffaec97 to 1d41fb5 Compare May 26, 2019 18:22
@davidungar
Copy link
Contributor Author

@swift-ci please smoke test

@davidungar davidungar force-pushed the A5-7-ASTOOScope-rebased branch 2 times, most recently from 9979bfd to c3da9c4 Compare May 28, 2019 17:15
@davidungar davidungar changed the title DNM [NameLookup] ASTOOScope ontology [NameLookup] ASTOOScope ontology May 28, 2019
@davidungar davidungar force-pushed the A5-7-ASTOOScope-rebased branch from c3da9c4 to 40d811e Compare May 28, 2019 17:29
@davidungar
Copy link
Contributor Author

@swift-ci please smoke test os x platform

@gottesmm
Copy link
Contributor

This is a huge patch, are you going to split this up?

@davidungar davidungar force-pushed the A5-7-ASTOOScope-rebased branch from 40d811e to 663760e Compare May 28, 2019 17:48
Copy link
Contributor

@gottesmm gottesmm left a 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.

@@ -1,4 +1,5 @@
//===--- ASTScope.h - Swift AST Scope ---------------------------*- C++ -*-===//
//===--- ASTScopeImpl.h - Swift AST Object-Oriented Scope -----------*- C++
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misformatted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tx. Fixed

@@ -10,117 +11,75 @@
//
//===----------------------------------------------------------------------===//
//
// This file defines the ASTScope class and related functionality, which
// This file defines the ASTScopeImpl class ontology, which
Copy link
Contributor

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:

https://llvm.org/docs/CodingStandards.html#file-headers

Copy link
Contributor Author

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.

//===----------------------------------------------------------------------===//
#ifndef SWIFT_AST_AST_SCOPE_H
#define SWIFT_AST_AST_SCOPE_H
#ifndef SWIFT_AST_AST_OO_SCOPE_H
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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).

Copy link
Contributor Author

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


namespace ast_scope {
class ASTScopeImpl;
class GTXScope;
Copy link
Contributor

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).

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK--spelled it out.

@davidungar
Copy link
Contributor Author

@swift-ci please clean smoke test os x platform

@davidungar
Copy link
Contributor Author

@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 EditorPlaceHolder. Other than that, I don't see any way to split it up. It is code that is off-by-default, so I don't expect any destabilization. It is also code that is new, so I don't foresee any conflicts.

Copy link
Contributor

@gottesmm gottesmm left a 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.

@@ -4666,16 +4664,19 @@ class UnresolvedPatternExpr : public Expr {
class EditorPlaceholderExpr : public Expr {
Identifier Placeholder;
SourceLoc Loc;
SourceLoc TrailingAngleBracketLoc;
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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>;
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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> &

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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;
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Will do.

if (se)
ScopeCreator().visit(se, this, deferred);
}
void ASTScopeImpl::dispatchAndCreateIfNeeded(Decl *d, DeferredNodes &deferred) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

// If there's another conditional clause, add it as the child.
if (index + 1 < getContainingStatement()->getCond().size())
createSubtreeForNextConditionalClause(deferred);
else {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite right and fair!

createSubtree<ClosureBodyScope>(captureList, closureExpr);
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another inconsistency around newlines.

Copy link
Contributor Author

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.


public:
ClosureFinder(
llvm::function_ref<void(NullablePtr<CaptureListExpr>, ClosureExpr *)>
Copy link
Contributor

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::.

Copy link
Contributor Author

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.

: foundClosure(foundClosure) {}

std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
if (auto closure = dyn_cast<ClosureExpr>(E)) {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks!

@davidungar
Copy link
Contributor Author

@swift-ci please clean smoke test os x platform

@davidungar
Copy link
Contributor Author

@swift-ci please smoke test os x platform

@davidungar
Copy link
Contributor Author

@swift-ci please smoke test

@davidungar
Copy link
Contributor Author

@swift-ci please smoke test Linux platform

@davidungar davidungar merged commit 06527db into swiftlang:master Jun 14, 2019
@davidungar
Copy link
Contributor Author

Merged per offline conversation with @slavapestov

@compnerd
Copy link
Member

nkcsgexi added a commit to nkcsgexi/swift that referenced this pull request Jun 27, 2019
…tree

This is swift-5.1-branch only fix. The fix on master has been superseded by this pull
request: swiftlang#24594

rdar://52128292
@davidungar davidungar deleted the A5-7-ASTOOScope-rebased branch September 23, 2019 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants