提交 a9a49151 编写于 作者: H Heejae Chang 提交者: GitHub

added IVariableDeclarationStatement back to for loop's before condition (#22358)

* updated local decl

* put IVariableDeclarationStatement back to for loop before.

* put variable declaration syntax in variable declaration statement for for loop

* updated tests

* changed caller to call different APIs to create variable declaration when it can.

* changed to return IVaraibleDeclaration from CreateVariableDeclaration method

* addressed PR feedbacks
上级 311e9475
...@@ -264,6 +264,11 @@ private ImmutableArray<IOperation> GetIOperationChildren(BoundNode boundNode) ...@@ -264,6 +264,11 @@ private ImmutableArray<IOperation> GetIOperationChildren(BoundNode boundNode)
return builder.ToImmutableAndFree(); return builder.ToImmutableAndFree();
} }
private IVariableDeclaration CreateVariableDeclaration(BoundLocalDeclaration boundNode)
{
return (IVariableDeclaration)_cache.GetOrAdd(boundNode, n => CreateVariableDeclarationInternal((BoundLocalDeclaration)n, n.Syntax));
}
private IPlaceholderExpression CreateBoundDeconstructValuePlaceholderOperation(BoundDeconstructValuePlaceholder boundDeconstructValuePlaceholder) private IPlaceholderExpression CreateBoundDeconstructValuePlaceholderOperation(BoundDeconstructValuePlaceholder boundDeconstructValuePlaceholder)
{ {
SyntaxNode syntax = boundDeconstructValuePlaceholder.Syntax; SyntaxNode syntax = boundDeconstructValuePlaceholder.Syntax;
...@@ -1393,28 +1398,56 @@ private IInvalidStatement CreateBoundBadStatementOperation(BoundBadStatement bou ...@@ -1393,28 +1398,56 @@ private IInvalidStatement CreateBoundBadStatementOperation(BoundBadStatement bou
private IOperation CreateBoundLocalDeclarationOperation(BoundLocalDeclaration boundLocalDeclaration) private IOperation CreateBoundLocalDeclarationOperation(BoundLocalDeclaration boundLocalDeclaration)
{ {
if (boundLocalDeclaration.Syntax.Kind() == SyntaxKind.LocalDeclarationStatement) var node = boundLocalDeclaration.Syntax;
{ var kind = node.Kind();
LocalDeclarationStatementSyntax statement = (LocalDeclarationStatementSyntax)boundLocalDeclaration.Syntax;
VariableDeclaratorSyntax variableDeclarator = statement.Declaration.Variables.First(); SyntaxNode varStatement;
Lazy<ImmutableArray<IVariableDeclaration>> declarations = new Lazy<ImmutableArray<IVariableDeclaration>>(() => ImmutableArray.Create(CreateVariableDeclaration(boundLocalDeclaration, variableDeclarator))); SyntaxNode varDeclaration;
ITypeSymbol type = null; switch (kind)
Optional<object> constantValue = default(Optional<object>);
bool isImplicit = boundLocalDeclaration.WasCompilerGenerated;
return new LazyVariableDeclarationStatement(declarations, _semanticModel, statement, type, constantValue, isImplicit);
}
else
{ {
// we can get here if someone asked about 1 variable declarator on multi local declaration case SyntaxKind.LocalDeclarationStatement:
Debug.Assert(boundLocalDeclaration.Syntax.Kind() == SyntaxKind.VariableDeclarator); {
return CreateVariableDeclaration(boundLocalDeclaration, boundLocalDeclaration.Syntax); var statement = (LocalDeclarationStatementSyntax)node;
// this happen for simple int i = 0;
// var statement points to LocalDeclarationStatementSyntax
varStatement = statement;
// var declaration points to VariableDeclaratorSyntax
varDeclaration = statement.Declaration.Variables.First();
break;
}
case SyntaxKind.VariableDeclarator:
{
// this happen for 'for loop' initializer
// var statement points to VariableDeclarationSyntax
varStatement = node.Parent;
// var declaration points to VariableDeclaratorSyntax
varDeclaration = node;
break;
}
default:
{
Debug.Fail($"Unexpected syntax: {kind}");
// otherwise, they points to whatever bound nodes are pointing to.
varStatement = varDeclaration = node;
break;
}
} }
Lazy<ImmutableArray<IVariableDeclaration>> declarations = new Lazy<ImmutableArray<IVariableDeclaration>>(() => ImmutableArray.Create(CreateVariableDeclarationInternal(boundLocalDeclaration, varDeclaration)));
ITypeSymbol type = null;
Optional<object> constantValue = default(Optional<object>);
bool isImplicit = boundLocalDeclaration.WasCompilerGenerated;
return new LazyVariableDeclarationStatement(declarations, _semanticModel, varStatement, type, constantValue, isImplicit);
} }
private IVariableDeclarationStatement CreateBoundMultipleLocalDeclarationsOperation(BoundMultipleLocalDeclarations boundMultipleLocalDeclarations) private IVariableDeclarationStatement CreateBoundMultipleLocalDeclarationsOperation(BoundMultipleLocalDeclarations boundMultipleLocalDeclarations)
{ {
Lazy<ImmutableArray<IVariableDeclaration>> declarations = new Lazy<ImmutableArray<IVariableDeclaration>>(() => Lazy<ImmutableArray<IVariableDeclaration>> declarations = new Lazy<ImmutableArray<IVariableDeclaration>>(() =>
boundMultipleLocalDeclarations.LocalDeclarations.SelectAsArray(declaration => (IVariableDeclaration)Create(declaration))); boundMultipleLocalDeclarations.LocalDeclarations.SelectAsArray(declaration => (IVariableDeclaration)CreateVariableDeclaration(declaration)));
SyntaxNode syntax = boundMultipleLocalDeclarations.Syntax; SyntaxNode syntax = boundMultipleLocalDeclarations.Syntax;
ITypeSymbol type = null; ITypeSymbol type = null;
Optional<object> constantValue = default(Optional<object>); Optional<object> constantValue = default(Optional<object>);
......
...@@ -51,7 +51,7 @@ internal IArgument CreateArgumentOperation(ArgumentKind kind, IParameterSymbol p ...@@ -51,7 +51,7 @@ internal IArgument CreateArgumentOperation(ArgumentKind kind, IParameterSymbol p
isImplicit: expression.WasCompilerGenerated || argument == null); isImplicit: expression.WasCompilerGenerated || argument == null);
} }
private IVariableDeclaration CreateVariableDeclaration(BoundLocalDeclaration boundLocalDeclaration, SyntaxNode syntax) private IVariableDeclaration CreateVariableDeclarationInternal(BoundLocalDeclaration boundLocalDeclaration, SyntaxNode syntax)
{ {
return OperationFactory.CreateVariableDeclaration(boundLocalDeclaration.LocalSymbol, Create(boundLocalDeclaration.InitializerOpt), _semanticModel, syntax); return OperationFactory.CreateVariableDeclaration(boundLocalDeclaration.LocalSymbol, Create(boundLocalDeclaration.InitializerOpt), _semanticModel, syntax);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册