Skip to content

Commit f955378

Browse files
committed
Rename State -> Environment
1 parent 642bfbd commit f955378

File tree

1 file changed

+74
-72
lines changed

1 file changed

+74
-72
lines changed

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,8 +1022,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10221022
}
10231023
}
10241024

1025-
/** Provides the input to `MatchingWithState`. */
1026-
signature module MatchingWithStateInputSig {
1025+
/** Provides the input to `MatchingWithEnvironment`. */
1026+
signature module MatchingWithEnvironmentInputSig {
10271027
/**
10281028
* A position inside a declaration. For example, the integer position of a
10291029
* parameter inside a method or the return type of a method.
@@ -1068,10 +1068,10 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10681068
string toString();
10691069
}
10701070

1071-
/** A state to track during type matching. */
1071+
/** An environment to track during type matching. */
10721072
bindingset[this]
1073-
class State {
1074-
/** Gets a textual representation of this state. */
1073+
class Environment {
1074+
/** Gets a textual representation of this environment. */
10751075
bindingset[this]
10761076
string toString();
10771077
}
@@ -1094,17 +1094,17 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10941094
Type getTypeArgument(TypeArgumentPosition tapos, TypePath path);
10951095

10961096
/**
1097-
* Gets the inferred type at `path` for the position `apos` and state `state`
1097+
* Gets the inferred type at `path` for the position `apos` and environment `e`
10981098
* of this access.
10991099
*
11001100
* For example, if this access is the method call `M(42)`, then the inferred
11011101
* type at argument position `0` is `int`.
11021102
*/
1103-
bindingset[state]
1104-
Type getInferredType(State state, AccessPosition apos, TypePath path);
1103+
bindingset[e]
1104+
Type getInferredType(Environment e, AccessPosition apos, TypePath path);
11051105

1106-
/** Gets the declaration that this access targets in `state`. */
1107-
Declaration getTarget(State state);
1106+
/** Gets the declaration that this access targets in environment `e`. */
1107+
Declaration getTarget(Environment e);
11081108
}
11091109

11101110
/** Holds if `apos` and `dpos` match. */
@@ -1142,20 +1142,20 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
11421142
*
11431143
* Matching takes both base types and explicit type arguments into account.
11441144
*/
1145-
module MatchingWithState<MatchingWithStateInputSig Input> {
1145+
module MatchingWithEnvironment<MatchingWithEnvironmentInputSig Input> {
11461146
private import Input
11471147

11481148
/**
1149-
* Holds if `a` targets `target` in `state` and the type for `apos` at `path`
1149+
* Holds if `a` targets `target` in environment `e` and the type for `apos` at `path`
11501150
* in `a` is `t` after adjustment by `target`.
11511151
*/
11521152
pragma[nomagic]
11531153
private predicate adjustedAccessType(
1154-
Access a, State state, AccessPosition apos, Declaration target, TypePath path, Type t
1154+
Access a, Environment e, AccessPosition apos, Declaration target, TypePath path, Type t
11551155
) {
1156-
target = a.getTarget(state) and
1156+
target = a.getTarget(e) and
11571157
exists(TypePath path0, Type t0 |
1158-
t0 = a.getInferredType(state, apos, path0) and
1158+
t0 = a.getInferredType(e, apos, path0) and
11591159
adjustAccessType(apos, target, path0, t0, path, t)
11601160
)
11611161
}
@@ -1179,50 +1179,51 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
11791179
}
11801180

11811181
/**
1182-
* Holds if the type `t` at `path` of `a` in `state` matches the type parameter `tp`
1183-
* of `target`.
1182+
* Holds if the type `t` at `path` of `a` in environment `e` matches the type
1183+
* parameter `tp` of `target`.
11841184
*/
11851185
pragma[nomagic]
11861186
private predicate directTypeMatch(
1187-
Access a, State state, Declaration target, TypePath path, Type t, TypeParameter tp
1187+
Access a, Environment e, Declaration target, TypePath path, Type t, TypeParameter tp
11881188
) {
11891189
not exists(getTypeArgument(a, target, tp, _)) and
11901190
exists(AccessPosition apos, DeclarationPosition dpos, TypePath pathToTypeParam |
11911191
tp = target.getDeclaredType(dpos, pathToTypeParam) and
11921192
accessDeclarationPositionMatch(apos, dpos) and
1193-
adjustedAccessType(a, state, apos, target, pathToTypeParam.appendInverse(path), t)
1193+
adjustedAccessType(a, e, apos, target, pathToTypeParam.appendInverse(path), t)
11941194
)
11951195
}
11961196

11971197
private module AccessBaseType {
11981198
/**
1199-
* Holds if inferring types at `a` in `state` might depend on the type at
1199+
* Holds if inferring types at `a` in environment `e` might depend on the type at
12001200
* `path` of `apos` having `base` as a transitive base type.
12011201
*/
1202-
private predicate relevantAccess(Access a, State state, AccessPosition apos, Type base) {
1202+
private predicate relevantAccess(Access a, Environment e, AccessPosition apos, Type base) {
12031203
exists(Declaration target, DeclarationPosition dpos |
1204-
adjustedAccessType(a, state, apos, target, _, _) and
1204+
adjustedAccessType(a, e, apos, target, _, _) and
12051205
accessDeclarationPositionMatch(apos, dpos) and
12061206
declarationBaseType(target, dpos, base, _, _)
12071207
)
12081208
}
12091209

12101210
pragma[nomagic]
12111211
private Type inferTypeAt(
1212-
Access a, State state, AccessPosition apos, TypeParameter tp, TypePath suffix
1212+
Access a, Environment e, AccessPosition apos, TypeParameter tp, TypePath suffix
12131213
) {
1214-
relevantAccess(a, state, apos, _) and
1214+
relevantAccess(a, e, apos, _) and
12151215
exists(TypePath path0 |
1216-
result = a.getInferredType(state, apos, path0) and
1216+
result = a.getInferredType(e, apos, path0) and
12171217
path0.isCons(tp, suffix)
12181218
)
12191219
}
12201220

12211221
/**
12221222
* Holds if `baseMention` is a (transitive) base type mention of the
1223-
* type of `a` at position `apos` at path `pathToSub` in `state`, and
1224-
* `t` is mentioned (implicitly) at `path` inside `base`. For example,
1225-
* in
1223+
* type of `a` at position `apos` at path `pathToSub` in environment
1224+
* `e`, and `t` is mentioned (implicitly) at `path` inside `base`.
1225+
*
1226+
* For example, in
12261227
*
12271228
* ```csharp
12281229
* class C<T1> { }
@@ -1251,15 +1252,16 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
12511252
* `Base<C<T3>>` | `"T2.T1.T1"` | `int`
12521253
*/
12531254
predicate hasBaseTypeMention(
1254-
Access a, State state, AccessPosition apos, TypeMention baseMention, TypePath path, Type t
1255+
Access a, Environment e, AccessPosition apos, TypeMention baseMention, TypePath path,
1256+
Type t
12551257
) {
1256-
relevantAccess(a, state, apos, resolveTypeMentionRoot(baseMention)) and
1257-
exists(Type sub | sub = a.getInferredType(state, apos, TypePath::nil()) |
1258+
relevantAccess(a, e, apos, resolveTypeMentionRoot(baseMention)) and
1259+
exists(Type sub | sub = a.getInferredType(e, apos, TypePath::nil()) |
12581260
baseTypeMentionHasNonTypeParameterAt(sub, baseMention, path, t)
12591261
or
12601262
exists(TypePath prefix, TypePath suffix, TypeParameter tp |
12611263
baseTypeMentionHasTypeParameterAt(sub, baseMention, prefix, tp) and
1262-
t = inferTypeAt(a, state, apos, tp, suffix) and
1264+
t = inferTypeAt(a, e, apos, tp, suffix) and
12631265
path = prefix.append(suffix)
12641266
)
12651267
)
@@ -1268,39 +1270,39 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
12681270

12691271
private module AccessConstraint {
12701272
predicate relevantAccessConstraint(
1271-
Access a, State state, Declaration target, AccessPosition apos, TypePath path,
1273+
Access a, Environment e, Declaration target, AccessPosition apos, TypePath path,
12721274
Type constraint
12731275
) {
1274-
target = a.getTarget(state) and
1276+
target = a.getTarget(e) and
12751277
typeParameterConstraintHasTypeParameter(target, apos, path, constraint, _, _)
12761278
}
12771279

12781280
private newtype TRelevantAccess =
12791281
MkRelevantAccess(
1280-
Access a, State state, Declaration target, AccessPosition apos, TypePath path
1282+
Access a, Environment e, Declaration target, AccessPosition apos, TypePath path
12811283
) {
1282-
relevantAccessConstraint(a, state, target, apos, path, _)
1284+
relevantAccessConstraint(a, e, target, apos, path, _)
12831285
}
12841286

12851287
/**
1286-
* If the access `a` for `apos`, `state`, and `path` has an inferred type
1288+
* If the access `a` for `apos`, environment `e`, and `path` has an inferred type
12871289
* which type inference requires to satisfy some constraint.
12881290
*/
12891291
private class RelevantAccess extends MkRelevantAccess {
12901292
Access a;
1291-
State state;
1293+
Environment e;
12921294
Declaration target;
12931295
AccessPosition apos;
12941296
TypePath path;
12951297

1296-
RelevantAccess() { this = MkRelevantAccess(a, state, target, apos, path) }
1298+
RelevantAccess() { this = MkRelevantAccess(a, e, target, apos, path) }
12971299

12981300
Type getTypeAt(TypePath suffix) {
1299-
adjustedAccessType(a, state, apos, target, path.appendInverse(suffix), result)
1301+
adjustedAccessType(a, e, apos, target, path.appendInverse(suffix), result)
13001302
}
13011303

13021304
/** Holds if this relevant access should satisfy `constraint`. */
1303-
Type getConstraint() { relevantAccessConstraint(a, state, target, apos, path, result) }
1305+
Type getConstraint() { relevantAccessConstraint(a, e, target, apos, path, result) }
13041306

13051307
string toString() {
13061308
result = a.toString() + ", " + apos.toString() + ", " + path.toString()
@@ -1318,24 +1320,24 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13181320
}
13191321

13201322
predicate satisfiesConstraintType(
1321-
Access a, State state, Declaration target, AccessPosition apos, TypePath prefix,
1323+
Access a, Environment e, Declaration target, AccessPosition apos, TypePath prefix,
13221324
Type constraint, TypePath path, Type t
13231325
) {
13241326
SatisfiesConstraint<RelevantAccess, SatisfiesConstraintInput>::satisfiesConstraintType(MkRelevantAccess(a,
1325-
state, target, apos, prefix), constraint, path, t)
1327+
e, target, apos, prefix), constraint, path, t)
13261328
}
13271329
}
13281330

13291331
/**
1330-
* Holds if the type of `a` at `apos` in `state` has the base type `base`,
1332+
* Holds if the type of `a` at `apos` in environment `e` has the base type `base`,
13311333
* and when viewed as an element of that type has the type `t` at `path`.
13321334
*/
13331335
pragma[nomagic]
13341336
private predicate accessBaseType(
1335-
Access a, State state, AccessPosition apos, Type base, TypePath path, Type t
1337+
Access a, Environment e, AccessPosition apos, Type base, TypePath path, Type t
13361338
) {
13371339
exists(TypeMention tm |
1338-
AccessBaseType::hasBaseTypeMention(a, state, apos, tm, path, t) and
1340+
AccessBaseType::hasBaseTypeMention(a, e, apos, tm, path, t) and
13391341
base = resolveTypeMentionRoot(tm)
13401342
)
13411343
}
@@ -1353,8 +1355,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13531355
}
13541356

13551357
/**
1356-
* Holds if the (transitive) base type `t` at `path` of `a` in `state` for
1357-
* some `AccessPosition` matches the type parameter `tp`, which is used in
1358+
* Holds if the (transitive) base type `t` at `path` of `a` in environment `e`
1359+
* for some `AccessPosition` matches the type parameter `tp`, which is used in
13581360
* the declared types of `target`.
13591361
*
13601362
* For example, in
@@ -1386,27 +1388,27 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13861388
*/
13871389
pragma[nomagic]
13881390
private predicate baseTypeMatch(
1389-
Access a, State state, Declaration target, TypePath path, Type t, TypeParameter tp
1391+
Access a, Environment e, Declaration target, TypePath path, Type t, TypeParameter tp
13901392
) {
13911393
not exists(getTypeArgument(a, target, tp, _)) and
1392-
target = a.getTarget(state) and
1394+
target = a.getTarget(e) and
13931395
exists(AccessPosition apos, DeclarationPosition dpos, Type base, TypePath pathToTypeParam |
1394-
accessBaseType(a, state, apos, base, pathToTypeParam.appendInverse(path), t) and
1396+
accessBaseType(a, e, apos, base, pathToTypeParam.appendInverse(path), t) and
13951397
declarationBaseType(target, dpos, base, pathToTypeParam, tp) and
13961398
accessDeclarationPositionMatch(apos, dpos)
13971399
)
13981400
}
13991401

14001402
/**
1401-
* Holds if for `a` and corresponding `target` in `state`, the type parameter
1403+
* Holds if for `a` and corresponding `target` in environment `e`, the type parameter
14021404
* `tp` is matched by a type argument at the access with type `t` and type path
14031405
* `path`.
14041406
*/
14051407
pragma[nomagic]
14061408
private predicate explicitTypeMatch(
1407-
Access a, State state, Declaration target, TypePath path, Type t, TypeParameter tp
1409+
Access a, Environment e, Declaration target, TypePath path, Type t, TypeParameter tp
14081410
) {
1409-
target = a.getTarget(state) and
1411+
target = a.getTarget(e) and
14101412
t = getTypeArgument(a, target, tp, path)
14111413
}
14121414

@@ -1448,36 +1450,36 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
14481450

14491451
pragma[nomagic]
14501452
private predicate typeConstraintBaseTypeMatch(
1451-
Access a, State state, Declaration target, TypePath path, Type t, TypeParameter tp
1453+
Access a, Environment e, Declaration target, TypePath path, Type t, TypeParameter tp
14521454
) {
14531455
not exists(getTypeArgument(a, target, tp, _)) and
14541456
exists(Type constraint, AccessPosition apos, TypePath pathToTp, TypePath pathToTp2 |
14551457
typeParameterConstraintHasTypeParameter(target, apos, pathToTp2, constraint, pathToTp, tp) and
1456-
AccessConstraint::satisfiesConstraintType(a, state, target, apos, pathToTp2, constraint,
1458+
AccessConstraint::satisfiesConstraintType(a, e, target, apos, pathToTp2, constraint,
14571459
pathToTp.appendInverse(path), t)
14581460
)
14591461
}
14601462

14611463
pragma[inline]
14621464
private predicate typeMatch(
1463-
Access a, State state, Declaration target, TypePath path, Type t, TypeParameter tp
1465+
Access a, Environment e, Declaration target, TypePath path, Type t, TypeParameter tp
14641466
) {
14651467
// A type given at the access corresponds directly to the type parameter
14661468
// at the target.
1467-
explicitTypeMatch(a, state, target, path, t, tp)
1469+
explicitTypeMatch(a, e, target, path, t, tp)
14681470
or
14691471
// We can infer the type of `tp` from one of the access positions
1470-
directTypeMatch(a, state, target, path, t, tp)
1472+
directTypeMatch(a, e, target, path, t, tp)
14711473
or
14721474
// We can infer the type of `tp` by going up the type hiearchy
1473-
baseTypeMatch(a, state, target, path, t, tp)
1475+
baseTypeMatch(a, e, target, path, t, tp)
14741476
or
14751477
// We can infer the type of `tp` by a type constraint
1476-
typeConstraintBaseTypeMatch(a, state, target, path, t, tp)
1478+
typeConstraintBaseTypeMatch(a, e, target, path, t, tp)
14771479
}
14781480

14791481
/**
1480-
* Gets the inferred type of `a` at `path` for position `apos` and state `state`.
1482+
* Gets the inferred type of `a` at `path` for position `apos` and environment `e`.
14811483
*
14821484
* For example, in
14831485
*
@@ -1515,19 +1517,19 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
15151517
* `"T2.T1.T1"` | `int`
15161518
*/
15171519
pragma[nomagic]
1518-
Type inferAccessType(Access a, State state, AccessPosition apos, TypePath path) {
1520+
Type inferAccessType(Access a, Environment e, AccessPosition apos, TypePath path) {
15191521
exists(DeclarationPosition dpos | accessDeclarationPositionMatch(apos, dpos) |
15201522
// A suffix of `path` leads to a type parameter in the target
15211523
exists(Declaration target, TypePath prefix, TypeParameter tp, TypePath suffix |
15221524
tp = target.getDeclaredType(dpos, prefix) and
15231525
path = prefix.append(suffix) and
1524-
typeMatch(a, state, target, suffix, result, tp)
1526+
typeMatch(a, e, target, suffix, result, tp)
15251527
)
15261528
or
15271529
// `path` corresponds directly to a concrete type in the declaration
15281530
exists(Declaration target |
15291531
result = target.getDeclaredType(dpos, path) and
1530-
target = a.getTarget(state) and
1532+
target = a.getTarget(e) and
15311533
not result instanceof TypeParameter
15321534
)
15331535
)
@@ -1645,30 +1647,30 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
16451647
* Matching takes both base types and explicit type arguments into account.
16461648
*/
16471649
module Matching<MatchingInputSig Input> {
1648-
private module Inp implements MatchingWithStateInputSig {
1650+
private module Inp implements MatchingWithEnvironmentInputSig {
16491651
private import codeql.util.Unit
16501652
import Input
16511653

16521654
predicate adjustAccessType = Input::adjustAccessType/6;
16531655

1654-
class State = Unit;
1656+
class Environment = Unit;
16551657

16561658
final private class AccessFinal = Input::Access;
16571659

16581660
class Access extends AccessFinal {
1659-
Type getInferredType(State state, AccessPosition apos, TypePath path) {
1660-
exists(state) and
1661+
Type getInferredType(Environment e, AccessPosition apos, TypePath path) {
1662+
exists(e) and
16611663
result = super.getInferredType(apos, path)
16621664
}
16631665

1664-
Declaration getTarget(State state) {
1665-
exists(state) and
1666+
Declaration getTarget(Environment e) {
1667+
exists(e) and
16661668
result = super.getTarget()
16671669
}
16681670
}
16691671
}
16701672

1671-
private module M = MatchingWithState<Inp>;
1673+
private module M = MatchingWithEnvironment<Inp>;
16721674

16731675
import M
16741676

0 commit comments

Comments
 (0)