@@ -18,7 +18,7 @@ private module Annotations {
1818 /** An annotation on a type. */
1919 class TypeAnnotation extends TAnnotation {
2020 /** Gets the bit position in the bit-field, also used to order the annotations in the text format. */
21- abstract int getBit ( ) ;
21+ int getBit ( ) { none ( ) }
2222
2323 /** Gets the string prefixing the type, if any. */
2424 string getPrefix ( ) { none ( ) }
@@ -57,6 +57,41 @@ private module Annotations {
5757 override int getBit ( ) { result = 6 }
5858 }
5959
60+ /**
61+ * A structured type annotation representing type nullability.
62+ * For example, `IDictionary<string!,string?>?` has nullability `<!,?>?`.
63+ */
64+ class Nullability extends TypeAnnotation , TNullability {
65+ @nullability nullability ;
66+
67+ Nullability ( ) { this = TNullability ( nullability ) }
68+
69+ override string toString ( ) { result = getMemberString ( ) + getSelfNullability ( ) }
70+
71+ language [ monotonicAggregates]
72+ private string getMemberString ( ) {
73+ if nullability_member ( nullability , _, _)
74+ then
75+ result = "<" +
76+ concat ( int i , Nullability child |
77+ nullability_member ( nullability , i , getNullability ( child ) )
78+ |
79+ child .toString ( ) , "," order by i
80+ ) + ">"
81+ else result = ""
82+ }
83+
84+ /**
85+ * Gets a string representing the nullability, disregarding child nullability.
86+ * For example, `IDictionary<string!,string?>?` has nullability `?`.
87+ */
88+ string getSelfNullability ( ) { none ( ) }
89+
90+ override int getBit ( ) { none ( ) }
91+ }
92+
93+ @nullability getNullability ( Nullability n ) { n = TNullability ( result ) }
94+
6095 private newtype TAnnotations =
6196 TAnnotationFlags ( int flags , Nullability n ) {
6297 exists ( Element e |
@@ -75,9 +110,6 @@ private module Annotations {
75110 this = TAnnotationsNoFlags ( result )
76111 }
77112
78- /** Gets the nullability with no additional flags. */
79- Nullability getNoFlagsNullability ( ) { this = TAnnotationsNoFlags ( result ) }
80-
81113 /** Gets text to be displayed before the type. */
82114 string getTypePrefix ( ) {
83115 result = concat ( TypeAnnotation a |
@@ -114,44 +146,19 @@ private module Annotations {
114146 }
115147 }
116148
149+ /** Gets the nullability with no additional flags. */
150+ Nullability getNoFlagsNullability ( TypeAnnotations annotations ) {
151+ annotations = TAnnotationsNoFlags ( result )
152+ }
153+
117154 /**
118155 * Gets the `i`th child of type annotations `annotations`.
119156 * This is used to represent structured datatypes, where the structure
120157 * of the type annotation mirrors the structure of the annotated type.
121158 */
122159 bindingset [ i]
123160 TypeAnnotations getChild ( TypeAnnotations annotations , int i ) {
124- result .getNoFlagsNullability ( ) = getChildNullability ( annotations .getNullability ( ) , i )
125- }
126-
127- /**
128- * A structured type annotation representing type nullability.
129- * For example, `IDictionary<string!,string?>?` has nullability `<!,?>?`.
130- */
131- abstract class Nullability extends TypeAnnotation , TNullability {
132- @nullability nullability ;
133-
134- Nullability ( ) { this = TNullability ( nullability ) }
135-
136- @nullability getNullability ( ) { result = nullability }
137-
138- override string toString ( ) { result = getMemberString ( ) + getSelfNullability ( ) }
139-
140- language [ monotonicAggregates]
141- private string getMemberString ( ) {
142- if nullability_member ( nullability , _, _)
143- then
144- result = "<" +
145- concat ( int i , Nullability child |
146- nullability_member ( nullability , i , child .getNullability ( ) )
147- |
148- child .toString ( ) , "," order by i
149- ) + ">"
150- else result = ""
151- }
152-
153- /** Gets a string representing the nullability, disregarding child nullability. */
154- string getSelfNullability ( ) { none ( ) }
161+ getNoFlagsNullability ( result ) = getChildNullability ( annotations .getNullability ( ) , i )
155162 }
156163
157164 /**
@@ -162,8 +169,8 @@ private module Annotations {
162169 */
163170 bindingset [ i]
164171 Nullability getChildNullability ( Nullability n , int i ) {
165- if nullability_member ( n . getNullability ( ) , i , _)
166- then nullability_member ( n . getNullability ( ) , i , result . getNullability ( ) )
172+ if nullability_member ( getNullability ( n ) , i , _)
173+ then nullability_member ( getNullability ( n ) , i , getNullability ( result ) )
167174 else result = n
168175 }
169176
@@ -176,8 +183,6 @@ private module Annotations {
176183 ObliviousNullability ( ) { nullability instanceof @oblivious }
177184
178185 override string getSelfNullability ( ) { result = "_" }
179-
180- override int getBit ( ) { none ( ) }
181186 }
182187
183188 /**
@@ -220,7 +225,7 @@ private module Annotations {
220225 annotations = TAnnotationFlags ( getElementTypeFlags ( element ) , getElementNullability ( element ) )
221226 or
222227 not exists ( getElementTypeFlags ( element ) ) and
223- annotations . getNoFlagsNullability ( ) = getElementNullability ( element )
228+ getNoFlagsNullability ( annotations ) = getElementNullability ( element )
224229 ) and
225230 (
226231 type = element .( Assignable ) .getType ( )
@@ -250,16 +255,18 @@ private Annotations::Nullability getTypeParameterNullability(
250255 TypeParameterConstraints constraints , Type type
251256) {
252257 if specific_type_parameter_nullability ( constraints , getTypeRef ( type ) , _)
253- then specific_type_parameter_nullability ( constraints , getTypeRef ( type ) , result .getNullability ( ) )
258+ then
259+ specific_type_parameter_nullability ( constraints , getTypeRef ( type ) ,
260+ Annotations:: getNullability ( result ) )
254261 else (
255- specific_type_parameter_constraints ( constraints , type ) and
262+ specific_type_parameter_constraints ( constraints , getTypeRef ( type ) ) and
256263 result instanceof Annotations:: NoNullability
257264 )
258265}
259266
260267private Annotations:: Nullability getElementNullability ( @has_type_annotation element ) {
261268 if type_nullability ( element , _)
262- then type_nullability ( element , result . getNullability ( ) )
269+ then type_nullability ( element , Annotations :: getNullability ( result ) )
263270 else result instanceof Annotations:: NoNullability
264271}
265272
@@ -272,10 +279,10 @@ private newtype TAnnotatedType =
272279 annotations = Annotations:: getChild ( c .getAnnotations ( ) , i )
273280 )
274281 or
275- annotations . getNoFlagsNullability ( ) = getTypeParameterNullability ( _, type )
282+ Annotations :: getNoFlagsNullability ( annotations ) = getTypeParameterNullability ( _, type )
276283 or
277284 // All types have at least one annotated type
278- annotations . getNoFlagsNullability ( ) instanceof Annotations:: NoNullability
285+ Annotations :: getNoFlagsNullability ( annotations ) instanceof Annotations:: NoNullability
279286 or
280287 exists ( AnnotatedArrayType at |
281288 type = at .getType ( ) .( ArrayType ) .getElementType ( ) and
@@ -344,16 +351,17 @@ class AnnotatedType extends TAnnotatedType {
344351 /** Holds if this annotated type applies to element `e`. */
345352 predicate appliesTo ( Element e ) { Annotations:: elementTypeAnnotations ( e , type , annotations ) }
346353
347- /** Holds if this annotated type is the `ith` type argument of constructed generic 'g'. */
354+ /** Holds if this annotated type is the `i`th type argument of constructed generic 'g'. */
348355 predicate appliesToTypeArgument ( ConstructedGeneric g , int i ) {
349- this .getAnnotations ( ) . getNoFlagsNullability ( ) = Annotations:: getChildNullability ( getElementNullability ( g ) ,
356+ Annotations :: getNoFlagsNullability ( this .getAnnotations ( ) ) = Annotations:: getChildNullability ( getElementNullability ( g ) ,
350357 i ) and
351358 this .getType ( ) = g .getTypeArgument ( i )
352359 }
353360
354361 /** Holds if this annotated type applies to type parameter constraints `constraints`. */
355362 predicate appliesToTypeConstraint ( TypeParameterConstraints constraints ) {
356- this .getAnnotations ( ) .getNoFlagsNullability ( ) = getTypeParameterNullability ( constraints , type )
363+ Annotations:: getNoFlagsNullability ( this .getAnnotations ( ) ) = getTypeParameterNullability ( constraints ,
364+ type )
357365 }
358366}
359367
0 commit comments