Skip to content

Commit 71f5c8a

Browse files
committed
Shared: Add Util qlpack.
1 parent 594b7ef commit 71f5c8a

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Initial release. Includes common utility classes and modules: Unit, Boolean, and Option.

shared/util/codeql/util/Boolean.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** Provides the `Boolean` class. */
2+
3+
/**
4+
* A utility class that is equivalent to `boolean`.
5+
*
6+
* As opposed to `boolean`, this type does not require explicit binding.
7+
*/
8+
class Boolean extends boolean {
9+
Boolean() { this = [true, false] }
10+
}

shared/util/codeql/util/Option.qll

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/** Provides a module for constructing optional versions of types. */
2+
3+
/** A type with `toString`. */
4+
signature class TypeWithToString {
5+
string toString();
6+
}
7+
8+
/**
9+
* Constructs an `Option` type that is a disjoint union of the given type and an
10+
* additional singleton element.
11+
*/
12+
module Option<TypeWithToString T> {
13+
private newtype TOption =
14+
TNone() or
15+
TSome(T c)
16+
17+
/**
18+
* An option type. This is either a singleton `None` or a `Some` wrapping the
19+
* given type.
20+
*/
21+
class Option extends TOption {
22+
/** Gets a textual representation of this element. */
23+
string toString() {
24+
this = TNone() and result = "(none)"
25+
or
26+
exists(T c | this = TSome(c) and result = c.toString())
27+
}
28+
29+
/** Gets the wrapped element, if any. */
30+
T asSome() { this = TSome(result) }
31+
}
32+
33+
/** The singleton `None` element. */
34+
class None extends Option, TNone { }
35+
36+
/** A wrapper for the given type. */
37+
class Some extends Option, TSome { }
38+
39+
/** Gets the given element wrapped as an `Option`. */
40+
Some some(T c) { result = TSome(c) }
41+
}

shared/util/codeql/util/Unit.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** Provides the `Unit` class. */
2+
3+
/** The unit type. */
4+
private newtype TUnit = TMkUnit()
5+
6+
/** The trivial type with a single element. */
7+
class Unit extends TUnit {
8+
/** Gets a textual representation of this element. */
9+
string toString() { result = "unit" }
10+
}

shared/util/qlpack.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: codeql/util
2+
version: 0.0.1-dev
3+
groups: shared
4+
library: true
5+
dependencies:

0 commit comments

Comments
 (0)