Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions unified/ql/lib/codeql/unified/internal/ClassLikeElement.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Provides the `ClassLikeElement` class, the canonical representative of a class, struct, enum, etc.
*/
overlay[local?]
module;

private import unified
private import codeql.unified.internal.StaticNameBinding as N
private import codeql.unified.internal.mad.LegacyMaD as MaD

/**
* Gets the model-only type extended by the extension `ext`, provided that the extended
* type could not be resolved to a declaration.
*/
pragma[nomagic]
private MaD::TypeSelector getExtendedTypeSelector(ClassLikeDeclaration ext) {
not exists(N::getExtendedDeclaration(ext)) and
N::trackTypeSelectorThroughAliases(result) = N::getNodeFromRef(ext.getExtensionTarget())
}

cached
private newtype TClassLikeElement =
TClassLikeDeclarationElement(ClassLikeDeclaration decl) { not exists(decl.getExtensionTarget()) } or
TTypeSelectorElement(MaD::TypeSelector selector) {
selector != "" and
not exists(selector.getDeclaration()) and
not selector.isMetaType()
}

/**
* A class-like entity, such as a class, struct, enum, or protocol.
*
* This is the canonical representative of a class-like entity, unifying its
* primary declaration with any extensions of it. Types that are only known from
* a model (and have no declaration in the database) are represented by their type selector.
*/
class ClassLikeElement extends TClassLikeElement {
/** Gets the primary (non-extension) declaration of this class, if any. */
ClassLikeDeclaration asClassLikeDeclaration() { this = TClassLikeDeclarationElement(result) }

/** Gets the type selector of this class, if it is only known from a model. */
private MaD::TypeSelector asTypeSelector() { this = TTypeSelectorElement(result) }

/**
* Gets a declaration of this class, that is, either its primary declaration or an extension of it.
*/
cached
ClassLikeDeclaration getADeclaration() {
result = this.asClassLikeDeclaration()
or
N::getExtendedDeclaration(result) = this.asClassLikeDeclaration()
or
getExtendedTypeSelector(result) = this.asTypeSelector()
}

/** Gets a member of this class, including members declared in extensions. */
Member getAMember() { result = this.getADeclaration().getAMember() }

/** Gets the name of this class. */
string getName() {
result = this.asClassLikeDeclaration().getName()
or
result = this.asTypeSelector().getHead()
}

/** Gets the qualified name of this class. */
string getQualifiedName() {
result = this.asClassLikeDeclaration().getQualifiedName()
or
result = this.asTypeSelector()
}

/** Gets a textual representation of this element. */
string toString() {
result = this.asClassLikeDeclaration().toString()
or
result = this.asTypeSelector()
}

/** Gets the location of this element. */
Location getLocation() {
result = this.asClassLikeDeclaration().getLocation()
or
exists(this.asTypeSelector()) and
result instanceof EmptyLocation
}
}
91 changes: 75 additions & 16 deletions unified/ql/lib/codeql/unified/internal/FacadeAst.qll
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ module Unified {
private import Ast::Unified as G
import G

private string getTypeReferenceName(Expr expr) {
result = expr.(Identifier).getValue()
or
exists(MemberAccessExpr access |
expr = access and
result = getTypeReferenceName(access.getBase()) + "." + access.getMemberName()
)
or
exists(GenericTypeExpr generic |
expr = generic and
result = getTypeReferenceName(generic.getBase())
)
}

private string getExpandedTypeReferenceName(Expr expr) {
exists(TypeAliasDeclaration alias | NameBinding::getTypeFromRef(expr) = alias.getNameNode() |
result = getExpandedTypeReferenceName(alias.getType())
)
or
not exists(TypeAliasDeclaration alias | NameBinding::getTypeFromRef(expr) = alias.getNameNode()) and
result = getTypeReferenceName(expr)
}

/** The base class for all AST nodes. */
class AstNode extends G::AstNode {
/** Gets the file containing this AST node. */
Expand Down Expand Up @@ -225,13 +248,56 @@ module Unified {
string getQualifiedName(Member m) {
result = getQualifiedName(getDeclaringClass(m)) + "." + m.getNameOrFallback()
or
exists(ClassLikeDeclaration nested, ClassLikeDeclaration extension |
m = nested and
nested = extension.getAMember() and
not exists(getDeclaringClass(m))
|
result =
getExpandedTypeReferenceName(extension.getExtensionTarget()) + "." +
nested.getNameOrFallback()
)
or
not exists(getDeclaringClass(m)) and
not exists(ClassLikeDeclaration nested, ClassLikeDeclaration extension |
nested = m and
nested = extension.getAMember() and
exists(getExpandedTypeReferenceName(extension.getExtensionTarget()))
) and
result = m.getNameOrFallback()
}
}

private module QualifiedNameForToString<getDeclaringClassSig/1 getDeclaringClass> {
string getQualifiedName(Member m) {
result = getQualifiedName(getDeclaringClass(m)) + "." + m.getNameOrFallback()
or
exists(ClassLikeDeclaration nested, ClassLikeDeclaration extension |
m = nested and
nested = extension.getAMember() and
not exists(getDeclaringClass(m))
|
result =
getTypeReferenceName(extension.getExtensionTarget()) + "." + nested.getNameOrFallback()
Comment thread
asgerf marked this conversation as resolved.
Resolved
)
or
not exists(getDeclaringClass(m)) and
not exists(ClassLikeDeclaration nested, ClassLikeDeclaration extension |
nested = m and
nested = extension.getAMember() and
exists(getTypeReferenceName(extension.getExtensionTarget()))
) and
result = m.getNameOrFallback()
}
}

private ClassLikeDeclaration getParentClass(Member m) {
result.getAMemberIncludingExtensions() = m
exists(ClassLikeDeclaration cls | cls.getAMember() = m |
not exists(cls.getExtensionTarget()) and
result = cls
or
result = NameBinding::getExtendedDeclaration(cls)
Comment thread
asgerf marked this conversation as resolved.
Resolved
)
}

private ClassLikeDeclaration getEnclosingClass(Member m) { result.getAMember() = m }
Expand All @@ -241,7 +307,7 @@ module Unified {
* it suitable for use in `toString` predicates.
*/
private predicate getQualifiedNameForToString =
QualifiedName<getEnclosingClass/1>::getQualifiedName/1;
QualifiedNameForToString<getEnclosingClass/1>::getQualifiedName/1;

class ClassLikeDeclaration extends G::ClassLikeDeclaration {
/** Gets the name of this declaration. */
Expand All @@ -253,6 +319,12 @@ module Unified {
result = this.getABaseType().getType().(TypeMention::TypeMention).getClassLikeDeclaration()
}

/**
* Gets the class-like element declared by this declaration, or extended by it if
* this is an extension.
*/
U::ClassLikeElement getClassLikeElement() { this = result.getADeclaration() }

/**
* Gets the target of this extension class, or this class itself if it is not an extension.
*/
Expand All @@ -261,20 +333,7 @@ module Unified {
not exists(this.getExtensionTarget()) and
result = this
or
exists(NameBinding::NamespaceNode ns |
ns.isStaticMemberNamespace(result) and
ns.getAnExtension().isStaticMemberNamespace(this)
)
}

/**
* Gets a member of this class, including members from extensions.
*/
Member getAMemberIncludingExtensions() {
exists(ClassLikeDeclaration cls |
this = cls.resolve() and
result = cls.getAMember()
)
result = NameBinding::getExtendedDeclaration(this)
}

override string toString() {
Expand Down
62 changes: 50 additions & 12 deletions unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ class NamespaceNode extends NameBindingNode {
NameBindingNode getMember(string name) { result = this.getMember(name, _) }
}

private predicate typeAliasValueStep(NameBindingNode node1, NameBindingNode node2) {
exists(TypeAliasDeclaration decl |
node1 = getNodeFromRef(decl.getType()) and
node2.isIdentifier(decl.getNameNode())
)
}

private module TrackNamespaceInput implements TrackInputSig {
predicate shouldTrack(NameBindingNode node) { node instanceof NamespaceNode }

Expand All @@ -511,11 +518,7 @@ private module TrackNamespaceInput implements TrackInputSig {
}

predicate additionalValueStep(NameBindingNode node1, NameBindingNode node2) {
// Namespace-tracking goes through aliases, but declaration-tracking does not
exists(TypeAliasDeclaration decl |
node1 = getNodeFromRef(decl.getType()) and
node2.isIdentifier(decl.getNameNode())
)
typeAliasValueStep(node1, node2)
}
}

Expand Down Expand Up @@ -554,6 +557,16 @@ private module TrackNameBindingInput implements TrackInputSig {

private module TrackNameBinding = Track<TrackNameBindingInput>;

private module TrackTypeSelectorThroughAliasesInput implements TrackInputSig {
predicate shouldTrack(NameBindingNode node) { node.isTypeSelector(_) }

predicate additionalValueStep(NameBindingNode node1, NameBindingNode node2) {
typeAliasValueStep(node1, node2)
}
}

private module TrackTypeSelectorThroughAliases = Track<TrackTypeSelectorThroughAliasesInput>;

/** Gets a name-binding node that may refer to the given declaration. */
private NameBindingNode trackNameBinding(NameBinding decl) {
exists(NameBindingNode start |
Expand All @@ -570,6 +583,14 @@ NameBindingNode trackTypeSelector(MaD::TypeSelector type) {
)
}

/** Gets a name-binding node that may refer to the given model type through type aliases. */
NameBindingNode trackTypeSelectorThroughAliases(MaD::TypeSelector type) {
exists(NameBindingNode start |
start.isTypeSelector(type) and
result = TrackTypeSelectorThroughAliases::track(start)
)
}

/** Holds if `node` should be included in the debug view. */
private signature predicate relevantNodeSig(AstNode node);

Expand Down Expand Up @@ -709,15 +730,30 @@ private module FolderHeuristic {
}
}

private ClassLikeDeclaration resolveExtensionTarget(ClassLikeDeclaration cls) {
trackNameBinding(result.getNameNode()) = getNodeFromRef(cls.getExtensionTarget())
/**
* Gets the non-extension class-like declaration extended by the extension `ext`.
*
* Consider using `ClassLikeElement` instead of this predicate.
*/
cached
ClassLikeDeclaration getExtendedDeclaration(ClassLikeDeclaration ext) {
CachedStage::ref() and
not exists(result.getExtensionTarget()) and
exists(NamespaceNode ns |
ns.isStaticMemberNamespace(result) and
ns.getAnExtension().isStaticMemberNamespace(ext)
)
}

private ClassLikeDeclaration tryResolveExtensionTarget(ClassLikeDeclaration cls) {
result = resolveExtensionTarget(cls)
/**
* Gets the class in whose namespace unqualified member accesses in `scope` should be resolved:
* the extended declaration if `scope` is a resolvable extension, otherwise `scope` itself.
*/
private ClassLikeDeclaration getMemberLookupClass(ClassLikeDeclaration scope) {
result = getExtendedDeclaration(scope)
or
not exists(resolveExtensionTarget(cls)) and
result = cls
not exists(getExtendedDeclaration(scope)) and
result = scope
}

/**
Expand Down Expand Up @@ -749,7 +785,7 @@ private predicate unqualifiedMemberAccessCand(
exists(NamespaceNode namespace, string name |
name = access.getName() and
accessingClass =
tryResolveExtensionTarget(LocalNameBindingOutput::getAnUncertainScope(access, name))
getMemberLookupClass(LocalNameBindingOutput::getAnUncertainScope(access, name))
|
instanceAccess = true and
namespace.isInstanceMemberNamespace(accessingClass) and
Expand Down Expand Up @@ -940,5 +976,7 @@ private module CachedStage {
(exists(any(UnqualifiedMemberAccess u).getImplicitQualifierVariable()) implies any())
or
(exists(getStaticBindingTargetFromIdentifier(_)) implies any())
or
(exists(getExtendedDeclaration(_)) implies any())
}
}
Loading
Loading