@@ -8,36 +8,43 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
88{
99 class IsPattern : Expression < IsPatternExpressionSyntax >
1010 {
11- IsPattern ( ExpressionNodeInfo info ) : base ( info . SetKind ( ExprKind . IS ) )
11+ private IsPattern ( ExpressionNodeInfo info ) : base ( info . SetKind ( ExprKind . IS ) )
1212 {
1313 }
1414
15- protected override void Populate ( )
15+ private void PopulatePattern ( PatternSyntax pattern , TypeSyntax optionalType , SyntaxToken varKeyword , VariableDesignationSyntax designation )
1616 {
17- var constantPattern = Syntax . Pattern as ConstantPatternSyntax ;
18- if ( constantPattern != null )
17+ bool isVar = optionalType is null ;
18+ if ( ! isVar )
19+ Expressions . TypeAccess . Create ( cx , optionalType , this , 1 ) ;
20+
21+ if ( cx . Model ( pattern ) . GetDeclaredSymbol ( designation ) is ILocalSymbol symbol )
1922 {
20- Create ( cx , Syntax . Expression , this , 0 ) ;
21- Create ( cx , constantPattern . Expression , this , 3 ) ;
22- return ;
23- }
23+ var type = Type . Create ( cx , symbol . Type ) ;
2424
25- var pattern = Syntax . Pattern as DeclarationPatternSyntax ;
25+ if ( isVar )
26+ new Expression ( new ExpressionInfo ( cx , type , cx . Create ( varKeyword . GetLocation ( ) ) , ExprKind . TYPE_ACCESS , this , 1 , false , null ) ) ;
2627
27- if ( pattern == null )
28- {
29- throw new InternalError ( Syntax , "Is-pattern not handled" ) ;
28+ VariableDeclaration . Create ( cx , symbol , type , cx . Create ( pattern . GetLocation ( ) ) , cx . Create ( designation . GetLocation ( ) ) , isVar , this , 2 ) ;
3029 }
30+ }
3131
32+ protected override void Populate ( )
33+ {
3234 Create ( cx , Syntax . Expression , this , 0 ) ;
33- TypeAccess . Create ( cx , pattern . Type , this , 1 ) ;
34-
35- var symbol = cx . Model ( Syntax ) . GetDeclaredSymbol ( pattern . Designation ) as ILocalSymbol ;
36- if ( symbol != null )
35+ switch ( Syntax . Pattern )
3736 {
38- var type = Type . Create ( cx , symbol . Type ) ;
39- var isVar = pattern . Type . IsVar ;
40- VariableDeclaration . Create ( cx , symbol , type , cx . Create ( pattern . GetLocation ( ) ) , cx . Create ( pattern . Designation . GetLocation ( ) ) , isVar , this , 2 ) ;
37+ case ConstantPatternSyntax constantPattern :
38+ Create ( cx , constantPattern . Expression , this , 3 ) ;
39+ return ;
40+ case VarPatternSyntax varPattern :
41+ PopulatePattern ( varPattern , null , varPattern . VarKeyword , varPattern . Designation ) ;
42+ return ;
43+ case DeclarationPatternSyntax declPattern :
44+ PopulatePattern ( declPattern , declPattern . Type , default ( SyntaxToken ) , declPattern . Designation ) ;
45+ return ;
46+ default :
47+ throw new InternalError ( Syntax , "Is pattern not handled" ) ;
4148 }
4249 }
4350
0 commit comments