-
Notifications
You must be signed in to change notification settings - Fork 2.1k
unified: Build instance-member namespace and resolve unqualified lookups #22420
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
+262
−25
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ea600a9
unified: Add test for enums
asgerf 3cf499f
unified: Rename TExportedNamespace -> TStaticMemberNamespace
asgerf cd0afb9
unified: Add TInstanceMemberNamespace
asgerf 40a0291
unified: Store instance members in the new node
asgerf 2bc839d
unified: Add inheritance between instance namespaces
asgerf 5b31814
unified: Ensure member-namespaces are always NamespaceNodes
asgerf 981fc4d
unified: Add language hook for inheritance restriction
asgerf 2b28f75
unified: Expose handling of unqualified member accesses
asgerf b1ca110
unified: Add getStaticBindingTarget
asgerf fa36a11
unified: Sharpen set of candidates when measuring static name binding
asgerf 94165df
Apply batched suggestions from code review
asgerf 7190c8c
unified: Use getStaticBindingTarget in Candidate.getTarget
asgerf 3e6e3be
unified: Update qldoc
asgerf 4185f75
unified: Remove superfluous parens
asgerf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,8 @@ | |
| TIdentifier(Identifier n) or | ||
| TBulkImport(BulkImportingPattern p) or | ||
| TLocalName(LocalName local) or | ||
| TExportedNamespace(ClassLikeDeclaration cls) or | ||
| TStaticMemberNamespace(ClassLikeDeclaration cls) or | ||
| TInstanceMemberNamespace(ClassLikeDeclaration cls) or | ||
| TLocalNamespace(AstNode n) { | ||
| n = any(TopLevel t).getBody() or // Imported names come in scope here | ||
| n instanceof ClassLikeDeclaration | ||
|
|
@@ -31,8 +32,13 @@ | |
|
|
||
| predicate isLocalName(LocalName local) { this = TLocalName(local) } | ||
|
|
||
| /** Holds if this represents the set of static members available in the given namespace. */ | ||
| predicate isExportedNamespace(ClassLikeDeclaration cls) { this = TExportedNamespace(cls) } | ||
| /** Holds if this represents the set of static members available in the given class. */ | ||
| predicate isStaticMemberNamespace(ClassLikeDeclaration cls) { this = TStaticMemberNamespace(cls) } | ||
|
|
||
| /** Holds if this represents the set of instance members available in the given class. */ | ||
| predicate isInstanceMemberNamespace(ClassLikeDeclaration cls) { | ||
| this = TInstanceMemberNamespace(cls) | ||
| } | ||
|
|
||
| /** Holds if this represents the set of members that can be accessed unqualified within the given scope. */ | ||
| predicate isLocalNamespace(AstNode n) { this = TLocalNamespace(n) } | ||
|
|
@@ -56,7 +62,9 @@ | |
| or | ||
| this.isBulkImport(result) | ||
| or | ||
| this.isExportedNamespace(result) | ||
| this.isStaticMemberNamespace(result) | ||
| or | ||
| this.isInstanceMemberNamespace(result) | ||
| or | ||
| this.isLocalNamespace(result) | ||
| or | ||
|
|
@@ -71,7 +79,11 @@ | |
| exists(LocalName local | this.isLocalName(local) and result = "LocalName(" + local + ")") | ||
| or | ||
| exists(ClassLikeDeclaration cls | | ||
| this.isExportedNamespace(cls) and result = "ExportedNamespace(" + cls + ")" | ||
| this.isStaticMemberNamespace(cls) and result = "StaticMemberNamespace(" + cls + ")" | ||
| ) | ||
| or | ||
| exists(ClassLikeDeclaration cls | | ||
| this.isInstanceMemberNamespace(cls) and result = "InstanceMemberNamespace(" + cls + ")" | ||
| ) | ||
| or | ||
| exists(AstNode n | this.isLocalNamespace(n) and result = "LocalNamespace(" + n + ")") | ||
|
|
@@ -92,7 +104,13 @@ | |
| or | ||
| exists(LocalName local | this.isLocalName(local) and result = local.getLocation()) | ||
| or | ||
| exists(ClassLikeDeclaration cls | this.isExportedNamespace(cls) and result = cls.getLocation()) | ||
| exists(ClassLikeDeclaration cls | | ||
| this.isStaticMemberNamespace(cls) and result = cls.getLocation() | ||
| ) | ||
| or | ||
| exists(ClassLikeDeclaration cls | | ||
| this.isInstanceMemberNamespace(cls) and result = cls.getLocation() | ||
| ) | ||
| or | ||
| exists(AstNode n | this.isLocalNamespace(n) and result = n.getLocation()) | ||
| or | ||
|
|
@@ -157,12 +175,13 @@ | |
| predicate storeStep(NameBindingNode node1, string name, NameBindingNode node2) { | ||
| exists(ClassLikeDeclaration cls, Member member, NameDeclaration nameDecl | | ||
| member = cls.getAMember() and | ||
| not isInstanceMember(member) and | ||
| not isPrivateToLocalScope(nameDecl) and | ||
| nameDecl.getDeclaration() = member and | ||
| node1.isIdentifier(nameDecl) and | ||
| name = nameDecl.getName() and | ||
| node2.isExportedNamespace(cls) | ||
| if isInstanceMember(member) | ||
| then node2.isInstanceMemberNamespace(cls) | ||
| else node2.isStaticMemberNamespace(cls) | ||
| ) | ||
| or | ||
| exists(TopLevel top, Stmt stmt, NameDeclaration nameDecl | | ||
|
|
@@ -195,12 +214,12 @@ | |
| ) | ||
| or | ||
| exists(ClassLikeDeclaration cls | | ||
| node1.isExportedNamespace(cls) and | ||
| node1.isStaticMemberNamespace(cls) and | ||
| node2.isIdentifier(cls.getName()) | ||
| ) | ||
| or | ||
| exists(ClassLikeDeclaration cls | | ||
| node1.isExportedNamespace(cls) and | ||
| node1.isStaticMemberNamespace(cls) and | ||
| node2.isLocalNamespace(cls) | ||
| ) | ||
| or | ||
|
|
@@ -249,7 +268,7 @@ | |
| exists(ClassLikeDeclaration cls, BaseType base | | ||
| base = cls.getABaseType() and | ||
| supertype = getNodeFromRef(base.getType()) and | ||
| subtype.isExportedNamespace(cls) | ||
| subtype.isStaticMemberNamespace(cls) | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -295,9 +314,23 @@ | |
| ) | ||
| } | ||
|
|
||
| /** A name-binding node that has members. */ | ||
| /** Holds if the member represented by `node` can be inherited. */ | ||
| pragma[nomagic] | ||
| private predicate isInheritableMemberNode(NameBindingNode node) { | ||
| exists(NameDeclaration decl | | ||
| node.isIdentifier(decl) and | ||
| isInheritableMember(decl.getDeclaration()) | ||
| ) | ||
| } | ||
|
|
||
| /** A name-binding node that can have members. */ | ||
| class NamespaceNode extends NameBindingNode { | ||
| NamespaceNode() { storeStep(_, _, this) or inheritanceStep(_, this) } | ||
| NamespaceNode() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps the QL doc should say
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| storeStep(_, _, this) or | ||
| inheritanceStep(_, this) or | ||
| this.isInstanceMemberNamespace(_) or | ||
| this.isStaticMemberNamespace(_) | ||
| } | ||
|
|
||
| /** Gets a name-binding node that may refer to this namespace. */ | ||
| NameBindingNode ref() { result = TrackNamespace::track(this) } | ||
|
|
@@ -308,8 +341,27 @@ | |
| /** Holds if this namespace has an own-member of the given name */ | ||
| predicate hasOwnMember(string name) { exists(this.getOwnMember(name)) } | ||
|
|
||
| /** If this is the static namespace for a class, gets the corresponding instance namespace. */ | ||
|
hvitved marked this conversation as resolved.
|
||
| NamespaceNode toInstanceNamespace() { | ||
| exists(ClassLikeDeclaration cls | | ||
| this.isStaticMemberNamespace(cls) and | ||
| result.isInstanceMemberNamespace(cls) | ||
| ) | ||
| } | ||
|
|
||
| /** If this is the instance namespace for a class, gets the corresponding static namespace. */ | ||
|
hvitved marked this conversation as resolved.
|
||
| NamespaceNode toStaticNamespace() { result.toInstanceNamespace() = this } | ||
|
|
||
| private NamespaceNode getAnInheritanceParent1() { inheritanceStep(result.ref(), this) } | ||
|
|
||
| /** Gets a namespace from which this namespace inherits directly. */ | ||
| NamespaceNode getAnInheritanceParent() { inheritanceStep(result.ref(), this) } | ||
| NamespaceNode getAnInheritanceParent() { | ||
| result = this.getAnInheritanceParent1() | ||
| or | ||
| // `inheritanceStep` connects the static namespaces of classes. | ||
| // Add the corresponding inheritance relation between the instance namespaces. | ||
| result = this.toStaticNamespace().getAnInheritanceParent1().toInstanceNamespace() | ||
| } | ||
|
|
||
| /** Gets a namespace that directly inherits from this one. */ | ||
| NamespaceNode getAnInheritanceChild() { result.getAnInheritanceParent() = this } | ||
|
|
@@ -320,7 +372,8 @@ | |
| result = this.getOwnMember(name) | ||
| or | ||
| not this.hasOwnMember(name) and | ||
| result = this.getAnInheritanceParent().getMember(name) | ||
| result = this.getAnInheritanceParent().getMember(name) and | ||
| isInheritableMemberNode(result) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -506,3 +559,94 @@ | |
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `access` may resolve to `target` through the enclosing `accessingClass`. | ||
| * | ||
| * `instanceAccess` indicates whether this member should be accessed as an instance of `accessingClass` | ||
| * or as a static member. | ||
| */ | ||
| private predicate unqualifiedMemberAccessCand( | ||
|
|
||
| PotentialLocalNameAccess access, boolean instanceAccess, NameDeclaration target, | ||
| ClassLikeDeclaration accessingClass | ||
| ) { | ||
| not access instanceof NameDeclaration and | ||
| ( | ||
| // Resolved by local scoping | ||
| exists(LocalName local | | ||
| target.getLocalName() = local and | ||
| access.getLocalName() = local and | ||
| target.getDeclaration() = accessingClass.getAMember() | ||
| | | ||
| instanceAccess = true and | ||
| isInstanceMember(target.getDeclaration()) | ||
| or | ||
| instanceAccess = false and | ||
| isStaticMember(target.getDeclaration()) | ||
| ) | ||
| or | ||
| // Resolved in an uncertain scope | ||
| exists(NamespaceNode namespace, string name | | ||
| name = access.getName() and | ||
| accessingClass = LocalNameBindingOutput::getAnUncertainScope(access, name) | ||
| | | ||
| instanceAccess = true and | ||
| namespace.isInstanceMemberNamespace(accessingClass) and | ||
| namespace.getMember(name).isIdentifier(target) | ||
| or | ||
| instanceAccess = false and | ||
| namespace.isStaticMemberNamespace(accessingClass) and | ||
| namespace.getMember(name).isIdentifier(target) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private int unqualifiedMemberAccessDepth(PotentialLocalNameAccess access) { | ||
| result = max(AstNode scope | unqualifiedMemberAccessCand(access, _, _, scope) | scope.getDepth()) | ||
|
hvitved marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Holds if `access` is an unqualified access to `target`. | ||
| * | ||
| * `accessingClass` is the enclosing class in which the member was found, and | ||
| * `instanceAccess` indicates if it is an instance member or static member. | ||
| */ | ||
| predicate unqualifiedMemberAccess( | ||
| PotentialLocalNameAccess access, boolean instanceAccess, NameDeclaration target, | ||
| ClassLikeDeclaration accessingClass | ||
| ) { | ||
| unqualifiedMemberAccessCand(access, instanceAccess, target, accessingClass) and | ||
| accessingClass.getDepth() = unqualifiedMemberAccessDepth(access) | ||
| } | ||
|
|
||
| /** | ||
| * An identifier appearing in a unqualified position, referring to a member of an enclosing class. | ||
| */ | ||
| class UnqualifiedMemberAccess extends Identifier { | ||
| private boolean instanceAccess; | ||
| private NameDeclaration target; | ||
| private ClassLikeDeclaration accessingClass; | ||
|
|
||
| UnqualifiedMemberAccess() { | ||
| unqualifiedMemberAccess(this, instanceAccess, target, accessingClass) | ||
| } | ||
|
|
||
| /** Gets the name declaration of the member being accessed. */ | ||
| NameDeclaration getTarget() { result = target } | ||
|
|
||
| /** Gets the enclosing class whose (possibly inherited) member is being accessed. */ | ||
| ClassLikeDeclaration getAccessingClass() { result = accessingClass } | ||
|
|
||
| /** Holds if this is an instance access on the accessing class. */ | ||
| predicate isInstanceAccess() { instanceAccess = true } | ||
| } | ||
|
|
||
| /** Gets the declaration being accessed by `access`, as determined by static name binding. */ | ||
| NameDeclaration getStaticBindingTarget(Identifier access) { | ||
| // For unqualified accesses, use the shadowing-aware lookup | ||
| result = access.(UnqualifiedMemberAccess).getTarget() | ||
| or | ||
| // For others, just follow the name binding graph | ||
| not access instanceof UnqualifiedMemberAccess and | ||
| trackNameDeclaration(result).asIdentifier() = access | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.