diff --git a/shared/util/change-notes/2022-11-30-initial-version.md b/shared/util/change-notes/2022-11-30-initial-version.md new file mode 100644 index 00000000000..81893f23dfc --- /dev/null +++ b/shared/util/change-notes/2022-11-30-initial-version.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Initial release. Includes common utility classes and modules: Unit, Boolean, and Option. diff --git a/shared/util/codeql/util/Boolean.qll b/shared/util/codeql/util/Boolean.qll new file mode 100644 index 00000000000..84a91f763e4 --- /dev/null +++ b/shared/util/codeql/util/Boolean.qll @@ -0,0 +1,10 @@ +/** Provides the `Boolean` class. */ + +/** + * A utility class that is equivalent to `boolean`. + * + * As opposed to `boolean`, this type does not require explicit binding. + */ +class Boolean extends boolean { + Boolean() { this = [true, false] } +} diff --git a/shared/util/codeql/util/Option.qll b/shared/util/codeql/util/Option.qll new file mode 100644 index 00000000000..9382dc9555c --- /dev/null +++ b/shared/util/codeql/util/Option.qll @@ -0,0 +1,44 @@ +/** Provides a module for constructing optional versions of types. */ + +/** A type with `toString`. */ +signature class TypeWithToString { + string toString(); +} + +/** + * Constructs an `Option` type that is a disjoint union of the given type and an + * additional singleton element. + */ +module Option { + private newtype TOption = + TNone() or + TSome(T c) + + /** + * An option type. This is either a singleton `None` or a `Some` wrapping the + * given type. + */ + class Option extends TOption { + /** Gets a textual representation of this element. */ + string toString() { + this = TNone() and result = "(none)" + or + exists(T c | this = TSome(c) and result = c.toString()) + } + + /** Gets the wrapped element, if any. */ + T asSome() { this = TSome(result) } + + /** Holds if this option is the singleton `None`. */ + predicate isNone() { this = TNone() } + } + + /** The singleton `None` element. */ + class None extends Option, TNone { } + + /** A wrapper for the given type. */ + class Some extends Option, TSome { } + + /** Gets the given element wrapped as an `Option`. */ + Some some(T c) { result = TSome(c) } +} diff --git a/shared/util/codeql/util/Unit.qll b/shared/util/codeql/util/Unit.qll new file mode 100644 index 00000000000..e9611ed3df4 --- /dev/null +++ b/shared/util/codeql/util/Unit.qll @@ -0,0 +1,10 @@ +/** Provides the `Unit` class. */ + +/** The unit type. */ +private newtype TUnit = TMkUnit() + +/** The trivial type with a single element. */ +class Unit extends TUnit { + /** Gets a textual representation of this element. */ + string toString() { result = "unit" } +} diff --git a/shared/util/qlpack.yml b/shared/util/qlpack.yml new file mode 100644 index 00000000000..faee72042d6 --- /dev/null +++ b/shared/util/qlpack.yml @@ -0,0 +1,5 @@ +name: codeql/util +version: 0.0.1-dev +groups: shared +library: true +dependencies: