@@ -2,28 +2,24 @@ import csharp
22private import Conversion
33private import Caching
44
5- pragma [ noinline]
6- private Type getAProperSubType ( Type t ) {
7- not result instanceof DynamicType and
8- not result instanceof NullType and
9- result .isImplicitlyConvertibleTo ( t )
10- }
11-
125/**
6+ * INTERNAL: Do not use.
7+ *
138 * Provides an implementation of Global Value Numbering for types (see
149 * https://en.wikipedia.org/wiki/Global_value_numbering), where types are considered
1510 * equal modulo identity conversions and type parameters.
1611 */
17- private module Gvn {
12+ module Gvn {
1813 private class LeafType extends Type {
1914 LeafType ( ) {
2015 not exists ( this .getAChild ( ) ) and
21- not this instanceof TypeParameter
16+ not this instanceof TypeParameter and
17+ not this instanceof DynamicType
2218 }
2319 }
2420
2521 /** A type kind for a compound type. */
26- class CompoundTypeKindImpl extends TCompoundTypeKind {
22+ class CompoundTypeKind extends TCompoundTypeKind {
2723 /** Gets the number of type parameters for this kind. */
2824 int getNumberOfTypeParameters ( ) {
2925 this = TPointerTypeKind ( ) and result = 1
@@ -44,8 +40,8 @@ private module Gvn {
4440 or
4541 this = TNullableTypeKind ( ) and result = args + "?"
4642 or
47- exists ( int dim , int rnk | this = TArrayTypeKind ( dim , rnk ) |
48- result = args + "[" + dim + ", " + rnk + "]"
43+ exists ( int rnk | this = TArrayTypeKind ( _ , rnk ) |
44+ result = args + "[" + concat ( int i | i in [ 0 .. rnk - 2 ] | "," ) + "]"
4945 )
5046 or
5147 exists ( UnboundGenericType ugt | this = TConstructedType ( ugt ) |
@@ -61,7 +57,7 @@ private module Gvn {
6157 }
6258
6359 /** Gets the type kind for type `t`, if any. */
64- CompoundTypeKind getTypeKindImpl ( Type t ) {
60+ CompoundTypeKind getTypeKind ( Type t ) {
6561 result = TPointerTypeKind ( ) and t instanceof PointerType
6662 or
6763 result = TNullableTypeKind ( ) and t instanceof NullableType
@@ -86,11 +82,13 @@ private module Gvn {
8682 CompoundTypeKind getKind ( ) { none ( ) }
8783
8884 /** Gets a textual representation of this GVN. */
85+ cached
8986 string toString ( ) {
90- exists ( int i | this = TLeafGvnType ( i ) | result = i .toString ( ) )
87+ Stages:: UnificationStage:: forceCachingInSameStage ( ) and
88+ exists ( LeafType t | this = TLeafGvnType ( t ) | result = t .toString ( ) )
9189 or
9290 this instanceof TTypeParameterGvnType and
93- result = "<type parameter> "
91+ result = "T "
9492 or
9593 exists ( ConstructedGvnTypeList l | this = TConstructedGvnType ( l ) | result = l .toString ( ) )
9694 }
@@ -99,6 +97,8 @@ private module Gvn {
9997 Location getLocation ( ) { result instanceof EmptyLocation }
10098 }
10199
100+ class TypeParameterGvnType extends GvnType , TTypeParameterGvnType { }
101+
102102 class ConstructedGvnType extends GvnType , TConstructedGvnType {
103103 private ConstructedGvnTypeList l ;
104104
@@ -157,7 +157,7 @@ private module Gvn {
157157 args = concat ( int i |
158158 i in [ 0 .. k .getNumberOfTypeParameters ( ) - 1 ]
159159 |
160- this .getArg ( i ) .toString ( ) , ", " order by i
160+ this .getArg ( i ) .toString ( ) , "," order by i
161161 ) and
162162 result = k .toString ( args )
163163 )
@@ -356,8 +356,6 @@ private module Gvn {
356356 not result instanceof ConstructedGvnType
357357 }
358358
359- private predicate id ( LeafType t , int i ) = equivalenceRelation( convIdentity / 2 ) ( t , i )
360-
361359 cached
362360 private module Cached {
363361 cached
@@ -367,11 +365,11 @@ private module Gvn {
367365 TArrayTypeKind ( int dim , int rnk ) {
368366 exists ( ArrayType at | dim = at .getDimension ( ) and rnk = at .getRank ( ) )
369367 } or
370- TConstructedType ( UnboundGenericType ugt )
368+ TConstructedType ( UnboundGenericType ugt ) { exists ( ugt . getATypeParameter ( ) ) }
371369
372370 cached
373371 newtype TGvnType =
374- TLeafGvnType ( int i ) { id ( _ , i ) } or
372+ TLeafGvnType ( LeafType t ) or
375373 TTypeParameterGvnType ( ) or
376374 TConstructedGvnType ( ConstructedGvnTypeList l )
377375
@@ -385,7 +383,10 @@ private module Gvn {
385383 /** Gets the GVN for type `t`. */
386384 cached
387385 GvnType getGlobalValueNumber ( Type t ) {
388- result = TLeafGvnType ( any ( int i | id ( t , i ) ) )
386+ result = TLeafGvnType ( t )
387+ or
388+ t instanceof DynamicType and
389+ result = TLeafGvnType ( any ( ObjectType ot ) )
389390 or
390391 t instanceof TypeParameter and
391392 result = TTypeParameterGvnType ( )
@@ -430,10 +431,6 @@ private module Gvn {
430431 import Cached
431432}
432433
433- class CompoundTypeKind = Gvn:: CompoundTypeKindImpl ;
434-
435- predicate getTypeKind = Gvn:: getTypeKindImpl / 1 ;
436-
437434/** Provides definitions related to type unification. */
438435module Unification {
439436 /** A type parameter that is compatible with any type. */
@@ -557,19 +554,19 @@ module Unification {
557554
558555 cached
559556 predicate typeConstraintUnifiable ( TTypeConstraint ttc , Type t ) {
560- exists ( Type t0 | ttc = TTypeConstraint ( t0 ) | t = getAProperSubType ( t0 ) )
557+ exists ( Type t0 | ttc = TTypeConstraint ( t0 ) | implicitConversionRestricted ( t , t0 ) )
561558 or
562559 exists ( Type t0 , Type t1 | ttc = TTypeConstraint ( t0 ) and unifiable ( t0 , t1 ) |
563- t = getAProperSubType ( t1 )
560+ implicitConversionRestricted ( t , t1 )
564561 )
565562 }
566563
567564 cached
568565 predicate typeConstraintSubsumes ( TTypeConstraint ttc , Type t ) {
569- exists ( Type t0 | ttc = TTypeConstraint ( t0 ) | t = getAProperSubType ( t0 ) )
566+ exists ( Type t0 | ttc = TTypeConstraint ( t0 ) | implicitConversionRestricted ( t , t0 ) )
570567 or
571568 exists ( Type t0 , Type t1 | ttc = TTypeConstraint ( t0 ) and subsumes ( t0 , t1 ) |
572- t = getAProperSubType ( t1 )
569+ implicitConversionRestricted ( t , t1 )
573570 )
574571 }
575572 }
0 commit comments