@@ -26,13 +26,13 @@ static VariableDeclaration CreateSingle(Context cx, DeclarationExpressionSyntax
2626 if ( variableSymbol == null )
2727 {
2828 cx . ModelError ( node , "Failed to determine local variable" ) ;
29- return new VariableDeclaration ( cx , node , null , isVar , parent , child ) ;
29+ return Create ( cx , node , null , isVar , parent , child ) ;
3030 }
3131
3232 var type = Type . Create ( cx , variableSymbol . Type ) ;
3333 var location = cx . Create ( designation . GetLocation ( ) ) ;
3434
35- var ret = new VariableDeclaration ( cx , designation , type , isVar , parent , child ) ;
35+ var ret = Create ( cx , designation , type , isVar , parent , child ) ;
3636 cx . Try ( null , null , ( ) => LocalVariable . Create ( cx , variableSymbol , ret , isVar , location ) ) ;
3737 return ret ;
3838 }
@@ -41,7 +41,7 @@ static VariableDeclaration CreateSingle(Context cx, DeclarationExpressionSyntax
4141 /// Create a tuple expression representing a parenthesized variable declaration.
4242 /// That is, we consider `var (x, y) = ...` to be equivalent to `(var x, var y) = ...`.
4343 /// </summary>
44- static Expression CreateParenthesized ( Context cx , DeclarationExpressionSyntax node , ParenthesizedVariableDesignationSyntax designation , IExpressionParentEntity parent , int child )
44+ public static Expression CreateParenthesized ( Context cx , DeclarationExpressionSyntax node , ParenthesizedVariableDesignationSyntax designation , IExpressionParentEntity parent , int child )
4545 {
4646 var type = Type . Create ( cx , null ) ; // Should ideally be a corresponding tuple type
4747 var tuple = new Expression ( new ExpressionInfo ( cx , type , cx . Create ( node . GetLocation ( ) ) , ExprKind . TUPLE , parent , child , false , null ) ) ;
@@ -56,41 +56,73 @@ static Expression CreateParenthesized(Context cx, DeclarationExpressionSyntax no
5656 return tuple ;
5757 }
5858
59- static Expression Create ( Context cx , DeclarationExpressionSyntax node , VariableDesignationSyntax designation , IExpressionParentEntity parent , int child )
59+ public static Expression CreateParenthesized ( Context cx , VarPatternSyntax varPattern , ParenthesizedVariableDesignationSyntax designation , IExpressionParentEntity parent , int child )
6060 {
61- var single = designation as SingleVariableDesignationSyntax ;
62- if ( single != null )
63- return CreateSingle ( cx , node , single , parent , child ) ;
64-
65- var paren = designation as ParenthesizedVariableDesignationSyntax ;
66- if ( paren != null )
67- return CreateParenthesized ( cx , node , paren , parent , child ) ;
61+ var type = Type . Create ( cx , null ) ; // Should ideally be a corresponding tuple type
62+ var tuple = new Expression ( new ExpressionInfo ( cx , type , cx . Create ( varPattern . GetLocation ( ) ) , ExprKind . TUPLE , parent , child , false , null ) ) ;
6863
69- var discard = designation as DiscardDesignationSyntax ;
70- if ( discard != null )
64+ cx . Try ( null , null , ( ) =>
7165 {
72- var type = cx . Model ( node ) . GetTypeInfo ( node ) . Type ;
73- return new VariableDeclaration ( cx , node , Type . Create ( cx , type ) , node . Type . IsVar , parent , child ) ;
74- }
66+ var child0 = 0 ;
67+ foreach ( var variable in designation . Variables )
68+ switch ( variable )
69+ {
70+ case ParenthesizedVariableDesignationSyntax paren :
71+ CreateParenthesized ( cx , varPattern , paren , tuple , child0 ++ ) ;
72+ break ;
73+ case SingleVariableDesignationSyntax single :
74+ if ( cx . Model ( variable ) . GetDeclaredSymbol ( single ) is ILocalSymbol local )
75+ {
76+ var decl = Create ( cx , variable , Type . Create ( cx , local . Type ) , true , tuple , child0 ++ ) ;
77+ var id = single . Identifier ;
78+ var declSymbol = cx . Model ( single ) . GetDeclaredSymbol ( single ) ;
79+ var location = cx . Create ( id . GetLocation ( ) ) ;
80+ LocalVariable . Create ( cx , local , decl , true , location ) ;
81+ }
82+ else
83+ {
84+ throw new InternalError ( single , "Failed to access local variable" ) ;
85+ }
86+ break ;
87+ case DiscardDesignationSyntax discard :
88+ new Discard ( cx , discard , tuple , child0 ++ ) ;
89+ break ;
90+ default :
91+ throw new InternalError ( variable , "Unhandled designation type" ) ;
92+ }
93+ } ) ;
7594
76- cx . ModelError ( node , "Failed to determine designation type" ) ;
77- return new VariableDeclaration ( cx , node , null , node . Type . IsVar , parent , child ) ;
95+ return tuple ;
7896 }
7997
80- public static Expression Create ( Context cx , DeclarationExpressionSyntax node , IExpressionParentEntity parent , int child )
81- {
82- return Create ( cx , node , node . Designation , parent , child ) ;
83- }
8498
85- VariableDeclaration ( Context cx , CSharpSyntaxNode d , Type type , bool isVar , IExpressionParentEntity parent , int child )
86- : base ( new ExpressionInfo ( cx , type , cx . Create ( d . FixedLocation ( ) ) , ExprKind . LOCAL_VAR_DECL , parent , child , false , null ) )
99+ static Expression Create ( Context cx , DeclarationExpressionSyntax node , VariableDesignationSyntax designation , IExpressionParentEntity parent , int child )
87100 {
101+ switch ( designation )
102+ {
103+ case SingleVariableDesignationSyntax single :
104+ return CreateSingle ( cx , node , single , parent , child ) ;
105+ case ParenthesizedVariableDesignationSyntax paren :
106+ return CreateParenthesized ( cx , node , paren , parent , child ) ;
107+ case DiscardDesignationSyntax discard :
108+ var type = cx . Model ( discard ) . GetTypeInfo ( discard ) . Type ;
109+ return Create ( cx , node , Type . Create ( cx , type ) , node . Type . IsVar , parent , child ) ;
110+ default :
111+ cx . ModelError ( node , "Failed to determine designation type" ) ;
112+ return Create ( cx , node , null , node . Type . IsVar , parent , child ) ;
113+ }
88114 }
89115
116+ public static Expression Create ( Context cx , DeclarationExpressionSyntax node , IExpressionParentEntity parent , int child ) =>
117+ Create ( cx , node , node . Designation , parent , child ) ;
118+
119+ public static VariableDeclaration Create ( Context cx , CSharpSyntaxNode c , Type type , bool isVar , IExpressionParentEntity parent , int child ) =>
120+ new VariableDeclaration ( new ExpressionInfo ( cx , type , cx . Create ( c . FixedLocation ( ) ) , ExprKind . LOCAL_VAR_DECL , parent , child , false , null ) ) ;
121+
90122 public static VariableDeclaration Create ( Context cx , CatchDeclarationSyntax d , bool isVar , IExpressionParentEntity parent , int child )
91123 {
92124 var type = Type . Create ( cx , cx . Model ( d ) . GetDeclaredSymbol ( d ) . Type ) ;
93- var ret = new VariableDeclaration ( cx , d , type , isVar , parent , child ) ;
125+ var ret = Create ( cx , d , type , isVar , parent , child ) ;
94126 cx . Try ( d , null , ( ) =>
95127 {
96128 var id = d . Identifier ;
@@ -102,9 +134,9 @@ public static VariableDeclaration Create(Context cx, CatchDeclarationSyntax d, b
102134 return ret ;
103135 }
104136
105- public static VariableDeclaration Create ( Context cx , VariableDeclaratorSyntax d , Type type , bool isVar , IExpressionParentEntity parent , int child )
137+ public static VariableDeclaration CreateDeclarator ( Context cx , VariableDeclaratorSyntax d , Type type , bool isVar , IExpressionParentEntity parent , int child )
106138 {
107- var ret = new VariableDeclaration ( cx , d , type , isVar , parent , child ) ;
139+ var ret = Create ( cx , d , type , isVar , parent , child ) ;
108140 cx . Try ( d , null , ( ) =>
109141 {
110142 var id = d . Identifier ;
@@ -137,7 +169,7 @@ public static void Populate(Context cx, VariableDeclarationSyntax decl, IExpress
137169
138170 foreach ( var v in decl . Variables )
139171 {
140- VariableDeclaration . Create ( cx , v , type , decl . Type . IsVar , parent , child ) ;
172+ VariableDeclaration . CreateDeclarator ( cx , v , type , decl . Type . IsVar , parent , child ) ;
141173 child += childIncrement ;
142174 }
143175 }
0 commit comments