Skip to content

Commit 92447dc

Browse files
committed
C#: Copy dotnet.Namespace implementation.
1 parent 7ba25b2 commit 92447dc

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

csharp/ql/lib/semmle/code/csharp/Generics.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import Location
1717
import Namespace
1818
private import commons.QualifiedName
19-
private import dotnet
2019
private import TypeRef
2120

2221
/**

csharp/ql/lib/semmle/code/csharp/Namespace.qll

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Provides classes for namespaces. */
22

3+
private import semmle.code.csharp.commons.QualifiedName
34
import Element
45
import Type
56
private import dotnet
@@ -18,24 +19,44 @@ class TypeContainer extends Declaration, @type_container { }
1819
* }
1920
* ```
2021
*/
21-
class Namespace extends DotNet::Namespace, TypeContainer, Declaration, @namespace {
22+
class Namespace extends TypeContainer, Declaration, @namespace {
2223
override Namespace getParent() { result = this.getParentNamespace() }
2324

24-
override Namespace getParentNamespace() { parent_namespace(this, result) }
25+
/**
26+
* Gets the parent namespace, if any. For example the parent namespace of `System.IO`
27+
* is `System`. The parent namespace of `System` is the global namespace.
28+
*/
29+
Namespace getParentNamespace() { parent_namespace(this, result) }
2530

26-
override Namespace getAChildNamespace() { parent_namespace(result, this) }
31+
/**
32+
* Gets a child namespace, if any. For example `System.IO` is a child in
33+
* the namespace `System`.
34+
*/
35+
Namespace getAChildNamespace() { parent_namespace(result, this) }
2736

2837
override TypeContainer getChild(int i) {
2938
i = 0 and
3039
parent_namespace(result, this)
3140
}
3241

42+
/**
43+
* Holds if this namespace has the qualified name `qualifier`.`name`.
44+
*
45+
* For example if the qualified name is `System.Collections.Generic`, then
46+
* `qualifier`=`System.Collections` and `name`=`Generic`.
47+
*/
3348
deprecated override predicate hasQualifiedName(string qualifier, string name) {
34-
DotNet::Namespace.super.hasQualifiedName(qualifier, name)
49+
namespaceHasQualifiedName(this, qualifier, name)
3550
}
3651

52+
/**
53+
* Holds if this namespace has the qualified name `qualifier`.`name`.
54+
*
55+
* For example if the qualified name is `System.Collections.Generic`, then
56+
* `qualifier`=`System.Collections` and `name`=`Generic`.
57+
*/
3758
override predicate hasFullyQualifiedName(string qualifier, string name) {
38-
DotNet::Namespace.super.hasFullyQualifiedName(qualifier, name)
59+
namespaceHasQualifiedName(this, qualifier, name)
3960
}
4061

4162
/**
@@ -123,7 +144,28 @@ class Namespace extends DotNet::Namespace, TypeContainer, Declaration, @namespac
123144

124145
override Location getALocation() { result = this.getADeclaration().getALocation() }
125146

126-
override string toString() { result = DotNet::Namespace.super.toString() }
147+
/** Gets a textual representation of this namespace. */
148+
override string toString() { result = this.getFullName() }
149+
150+
/** Holds if this is the global namespace. */
151+
final predicate isGlobalNamespace() { this.getName() = "" }
152+
153+
/** Gets the simple name of this namespace, for example `IO` in `System.IO`. */
154+
final override string getName() { namespaces(this, result) }
155+
156+
final override string getUndecoratedName() { namespaces(this, result) }
157+
158+
override string getAPrimaryQlClass() { result = "Namespace" }
159+
160+
/**
161+
* Get the fully qualified name of this namespace.
162+
*/
163+
string getFullName() {
164+
exists(string namespace, string name |
165+
namespaceHasQualifiedName(this, namespace, name) and
166+
result = getQualifiedName(namespace, name)
167+
)
168+
}
127169
}
128170

129171
/**

0 commit comments

Comments
 (0)