Skip to content
Draft
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
8 changes: 7 additions & 1 deletion unified/extractor/src/languages/swift/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,13 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
=>
(member_access_expr base: {base} member_name_node: (identifier #{name}))
),
// A metatype (`T.Type`, `P.Protocol`) is represented like a qualified type,
// as a member access on the base type.
rule!(
(metatypeType baseType: @base metatypeSpecifier: @@specifier)
=>
(member_access_expr base: {base} member_name_node: (identifier #{specifier}))
),
// Sugared types desugar to `generic_type_expr`: `T?` -> Optional<T>,
// `[T]` -> Array<T>, `[K: V]` -> Dictionary<K, V>.
rule!(
Expand Down Expand Up @@ -1845,7 +1852,6 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
rule!((inlineArrayType) => (unsupported_node "")),
rule!((keyPathExpr) => (unsupported_node "")),
rule!((macroDecl) => (unsupported_node "")),
rule!((metatypeType) => (unsupported_node "")),
rule!((namedOpaqueReturnType) => (unsupported_node "")),
rule!((operatorDecl) => (unsupported_node "")),
rule!((packElementExpr) => (unsupported_node "")),
Expand Down
130 changes: 130 additions & 0 deletions unified/extractor/tests/corpus/swift/types/metatype.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
func f(a: Foo.Type, b: Outer.Inner.Type, c: [Int].Type, d: P.Protocol) {}

---

sourceFile
endOfFileToken: endOfFile
statements:
codeBlockItem
item:
functionDecl
attributes:
body:
codeBlock
leftBrace: {
rightBrace: }
statements:
modifiers:
name: identifier "f"
signature:
functionSignature
parameterClause:
functionParameterClause
leftParen: (
rightParen: )
parameters:
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
metatypeType
baseType:
identifierType
name: identifier "Foo"
period: .
metatypeSpecifier: Type
firstName: identifier "a"
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
metatypeType
baseType:
memberType
name: identifier "Inner"
baseType:
identifierType
name: identifier "Outer"
period: .
period: .
metatypeSpecifier: Type
firstName: identifier "b"
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
metatypeType
baseType:
arrayType
leftSquare: [
rightSquare: ]
element:
identifierType
name: identifier "Int"
period: .
metatypeSpecifier: Type
firstName: identifier "c"
functionParameter
colon: :
attributes:
modifiers:
type:
metatypeType
baseType:
identifierType
name: identifier "P"
period: .
metatypeSpecifier: Protocol
firstName: identifier "d"
funcKeyword: func

---

top_level source="⟨body⟩"
body:
block source="⟨stmt⟩"
stmt:
function_declaration source="func ⟨name_node⟩(⟨parameter⟩, ⟨parameter⟩, ⟨parameter⟩, ⟨parameter⟩) ⟨body⟩"
name_node: identifier "f" source="f"
parameter:
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "a" source="a"
type:
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base: identifier "Foo" source="Foo"
member_name_node: identifier "Type" source="Type"
pattern: identifier "a" source="a"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "b" source="b"
type:
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base:
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base: identifier "Outer" source="Outer"
member_name_node: identifier "Inner" source="Inner"
member_name_node: identifier "Type" source="Type"
pattern: identifier "b" source="b"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "c" source="c"
type:
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base:
generic_type_expr source="[⟨type_argument⟩]"
base: identifier "Array" source=""
type_argument: identifier "Int" source="Int"
member_name_node: identifier "Type" source="Type"
pattern: identifier "c" source="c"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "d" source="d"
type:
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base: identifier "P" source="P"
member_name_node: identifier "Protocol" source="Protocol"
pattern: identifier "d" source="d"
body: block "{}" source="{}"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
func f(a: Foo.Type, b: Outer.Inner.Type, c: [Int].Type, d: P.Protocol) {}
2 changes: 2 additions & 0 deletions unified/ql/lib/codeql/unified/internal/AnalysisQuality.qll
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ module StaticNameResolutionStats implements EntityStatsSig {
NameBindingNode getTarget() {
result.asIdentifier() = getStaticBindingTargetFromIdentifier(this)
or
result = this.(UnqualifiedMemberAccess).getTargetNode()
or
result.isModuleScopeNode(_) and
result.(NamespaceNode).ref().isIdentifier(this)
or
Expand Down
86 changes: 86 additions & 0 deletions unified/ql/lib/codeql/unified/internal/ClassLikeElement.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 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::trackTypeSelector(result) = N::getNodeFromRef(ext.getExtensionTarget())
}

cached
private newtype TClassLikeElement =
TClassLikeDeclarationElement(ClassLikeDeclaration decl) { not exists(decl.getExtensionTarget()) } or
TTypeSelectorElement(MaD::TypeSelector selector) {
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. */
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
}
}
28 changes: 13 additions & 15 deletions unified/ql/lib/codeql/unified/internal/FacadeAst.qll
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ module Unified {
}

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

private ClassLikeDeclaration getEnclosingClass(Member m) { result.getAMember() = m }
Expand All @@ -251,6 +256,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 @@ -259,20 +270,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
Loading
Loading