Skip to content

Commit cf89fae

Browse files
committed
just use TypeExpr to resolve modules
1 parent dce817e commit cf89fae

File tree

4 files changed

+32
-65
lines changed

4 files changed

+32
-65
lines changed

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,17 +2191,14 @@ class DontCare extends TDontCare, Expr {
21912191
override string getAPrimaryQlClass() { result = "DontCare" }
21922192
}
21932193

2194-
/** A reference to a module as part of a parameterized module (or it's instantiation) */
2195-
class ModuleParameterRef extends ModuleRef, TModuleParameterRef {
2196-
QL::TypeExpr type;
2197-
2198-
ModuleParameterRef() { this = TModuleParameterRef(type) }
2199-
2194+
/**
2195+
* A type expression seen as a reference to a module as part of a parameterized module (or it's instantiation).
2196+
* This might not be a reference to a module, but we assume so until we find out in the resolve phase.
2197+
*/
2198+
class ModuleParameterRef extends ModuleRef instanceof TypeExpr {
22002199
final override FileOrModule getResolvedModule() { resolveModuleRef(this, result) }
22012200

2202-
override string getName() { result = type.getName().getValue() }
2203-
2204-
override string getAPrimaryQlClass() { result = "ModuleParameterRef" }
2201+
override string getName() { result = TypeExpr.super.getClassName() }
22052202
}
22062203

22072204
/** A module expression. Such as `DataFlow` in `DataFlow::Node` */
@@ -2219,7 +2216,15 @@ class ModuleExpr extends TModuleExpr, ModuleRef {
22192216
*
22202217
* is `Bar`.
22212218
*/
2222-
override string getName() { result = getNameForModuleExpr(me) }
2219+
override string getName() {
2220+
result = me.getName().(QL::SimpleId).getValue()
2221+
or
2222+
not exists(me.getName()) and result = me.getChild().(QL::SimpleId).getValue()
2223+
or
2224+
exists(QL::ModuleInstantiation instantiation | instantiation.getParent() = me |
2225+
result = instantiation.getName().getChild().getValue()
2226+
)
2227+
}
22232228

22242229
/**
22252230
* Gets the qualifier of this module expression. For example, the qualifier of

ql/ql/src/codeql_ql/ast/internal/AstNodes.qll

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ newtype TAstNode =
1919
TNewType(QL::Datatype dt) or
2020
TNewTypeBranch(QL::DatatypeBranch branch) or
2121
TImport(QL::ImportDirective imp) or
22-
// splitting up the TypeExpr based on whether they are a reference to a type or to a module, with some duplicates.
23-
TType(QL::TypeExpr type) { not isDefinitelyModuleParameter(type) } or
24-
TModuleParameterRef(QL::TypeExpr type) {
25-
isDefinitelyModuleParameter(type) or mightBeModuleParameter(type)
26-
} or
22+
TType(QL::TypeExpr type) or
2723
TDisjunction(QL::Disjunction disj) or
2824
TConjunction(QL::Conjunction conj) or
2925
TComparisonFormula(QL::CompTerm comp) or
@@ -87,43 +83,11 @@ class TExpr =
8783

8884
class TCall = TPredicateCall or TMemberCall or TNoneCall or TAnyCall;
8985

90-
class TModuleRef = TImport or TModuleExpr or TModuleParameterRef;
86+
class TModuleRef = TImport or TModuleExpr or TType;
9187

9288
class TYamlNode = TYamlCommemt or TYamlEntry or TYamlKey or TYamlListitem or TYamlValue;
9389

94-
class TSignatureExpr = TPredicateExpr or TType or TModuleParameterRef;
95-
96-
private predicate isDefinitelyModuleParameter(QL::TypeExpr type) {
97-
// the signature of a parameterized module
98-
exists(QL::SignatureExpr expr, QL::Module m, QL::ModuleParam param, string name |
99-
param = m.getParameter(_) and
100-
name = param.getParameter().getValue() and
101-
expr = param.getSignature() and
102-
type = expr.getTypeExpr() and
103-
// we have a ref to it, to confirm that it's actually a module.
104-
exists(QL::ModuleExpr ref | getNameForModuleExpr(ref) = name | ref.getParent+() = m)
105-
)
106-
or
107-
// the implements clause of a module.
108-
exists(QL::Module qlmod | qlmod.getImplements(_).getTypeExpr() = type)
109-
}
110-
111-
private predicate mightBeModuleParameter(QL::TypeExpr type) {
112-
// or it's in an instantiation. The only way to know for sure if it's a module ref or not is to try, so we just have duplicates in the AST.
113-
// we spuriously have both TypeExpr and ModuleParameterRef for the same thing.
114-
exists(QL::ModuleInstantiation inst | type = inst.getChild(_).getTypeExpr())
115-
}
116-
117-
// ensuring non-monotonic recursion by outlining predicate out of `ModuleExpr`
118-
string getNameForModuleExpr(QL::ModuleExpr me) {
119-
result = me.getName().(QL::SimpleId).getValue()
120-
or
121-
not exists(me.getName()) and result = me.getChild().(QL::SimpleId).getValue()
122-
or
123-
exists(QL::ModuleInstantiation instantiation | instantiation.getParent() = me |
124-
result = instantiation.getName().getChild().getValue()
125-
)
126-
}
90+
class TSignatureExpr = TPredicateExpr or TType;
12791

12892
/** DEPRECATED: Alias for TYamlNode */
12993
deprecated class TYAMLNode = TYamlNode;
@@ -212,8 +176,6 @@ QL::AstNode toQL(AST::AstNode n) {
212176
or
213177
n = TNewTypeBranch(result)
214178
or
215-
n = TModuleParameterRef(result)
216-
or
217179
n = TImport(result)
218180
or
219181
n = TType(result)

ql/ql/src/codeql_ql/ast/internal/Module.qll

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ private class ContainerOrModule extends TContainerOrModule {
2424
this = TFolder(_) and result = "folder"
2525
}
2626

27-
/** Gets the module for this imported module. */
28-
Module asModule() { this = TModule(result) }
27+
/** Gets the module for this imported module. */
28+
Module asModule() { this = TModule(result) }
2929

30-
/** Gets the file for this file. */
31-
File asFile() { this = TFile(result) }
30+
/** Gets the file for this file. */
31+
File asFile() { this = TFile(result) }
3232
}
3333

3434
private class TFileOrModule = TFile or TModule;
@@ -115,7 +115,6 @@ class Module_ extends FileOrModule, TModule {
115115
}
116116

117117
// class ModuleRef = AstNodes::TModuleExpr or AstNodes::TType;
118-
119118
private predicate resolveQualifiedName(Import imp, ContainerOrModule m, int i) {
120119
not m = TFile(any(File f | f.getExtension() = "ql")) and
121120
exists(string q | q = imp.getQualifiedName(i) |
@@ -209,7 +208,7 @@ private module Cached {
209208

210209
/** Holds if module expression `me` resolves to `m`. */
211210
cached
212-
predicate resolveModuleRef(ModuleRef me, FileOrModule m) { // TODO: name.
211+
predicate resolveModuleRef(ModuleRef me, FileOrModule m) {
213212
not m = TFile(any(File f | f.getExtension() = "ql")) and
214213
not exists(me.(ModuleExpr).getQualifier()) and
215214
exists(ContainerOrModule enclosing, string name | resolveModuleRefHelper(me, enclosing, name) |
@@ -257,17 +256,17 @@ private predicate definesModule(
257256
or
258257
m = TModule(any(Module mod | public = getPublicBool(mod)))
259258
)
260-
or
259+
or
261260
// signature module in a paramertized module
262-
exists(Module mod, SignatureExpr sig, ModuleParameterRef ty, int i |
261+
exists(Module mod, SignatureExpr sig, ModuleParameterRef ty, int i |
263262
mod = container.asModule() and
264263
mod.hasParameter(i, name, sig) and
265264
public = false and
266-
ty = sig.asModuleRef()
267-
|
265+
ty = sig.asModuleRef()
266+
|
268267
m = ty.getResolvedModule()
269268
or
270-
exists(ModuleExpr inst | inst.getResolvedModule().asModule() = mod |
269+
exists(ModuleExpr inst | inst.getResolvedModule().asModule() = mod |
271270
m = inst.getArgument(i).asModuleRef().getResolvedModule()
272271
)
273272
)
@@ -307,12 +306,14 @@ module ModConsistency {
307306
.regexpMatch(".*/(test|examples|ql-training|recorded-call-graph-metrics)/.*")
308307
}
309308

310-
query predicate noResolveModuleRef(ModuleRef me) { // TODO: name?
309+
query predicate noResolveModuleRef(ModuleRef me) {
311310
not exists(me.getResolvedModule()) and
312311
not me.getLocation()
313312
.getFile()
314313
.getAbsolutePath()
315-
.regexpMatch(".*/(test|examples|ql-training|recorded-call-graph-metrics)/.*")
314+
.regexpMatch(".*/(test|examples|ql-training|recorded-call-graph-metrics)/.*") and
315+
// this ModuleRef might really be a type.
316+
not exists(me.(TypeExpr).getResolvedType())
316317
}
317318

318319
query predicate multipleResolve(Import imp, int c, ContainerOrModule m) {

ql/ql/src/codeql_ql/ast/internal/Type.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ module TyConsistency {
350350
.getAbsolutePath()
351351
.regexpMatch(".*/(test|examples|ql-training|recorded-call-graph-metrics)/.*") and
352352
// we have some duplicate with moduleRef, so that might be resolved correctly.
353-
// TODO: Collapse both ModuleRef and TypeExpr into one class?
354353
not exists(ModuleRef ref | AstNodes::toQL(te) = AstNodes::toQL(ref) |
355354
exists(ref.getResolvedModule())
356355
)

0 commit comments

Comments
 (0)