提交 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)
return builder.ToImmutableAndFree();
}
private IVariableDeclaration CreateVariableDeclaration(BoundLocalDeclaration boundNode)
{
return (IVariableDeclaration)_cache.GetOrAdd(boundNode, n => CreateVariableDeclarationInternal((BoundLocalDeclaration)n, n.Syntax));
}
private IPlaceholderExpression CreateBoundDeconstructValuePlaceholderOperation(BoundDeconstructValuePlaceholder boundDeconstructValuePlaceholder)
{
SyntaxNode syntax = boundDeconstructValuePlaceholder.Syntax;
......@@ -1393,28 +1398,56 @@ private IInvalidStatement CreateBoundBadStatementOperation(BoundBadStatement bou
private IOperation CreateBoundLocalDeclarationOperation(BoundLocalDeclaration boundLocalDeclaration)
{
if (boundLocalDeclaration.Syntax.Kind() == SyntaxKind.LocalDeclarationStatement)
{
LocalDeclarationStatementSyntax statement = (LocalDeclarationStatementSyntax)boundLocalDeclaration.Syntax;
VariableDeclaratorSyntax variableDeclarator = statement.Declaration.Variables.First();
Lazy<ImmutableArray<IVariableDeclaration>> declarations = new Lazy<ImmutableArray<IVariableDeclaration>>(() => ImmutableArray.Create(CreateVariableDeclaration(boundLocalDeclaration, variableDeclarator)));
ITypeSymbol type = null;
Optional<object> constantValue = default(Optional<object>);
bool isImplicit = boundLocalDeclaration.WasCompilerGenerated;
return new LazyVariableDeclarationStatement(declarations, _semanticModel, statement, type, constantValue, isImplicit);
}
else
var node = boundLocalDeclaration.Syntax;
var kind = node.Kind();
SyntaxNode varStatement;
SyntaxNode varDeclaration;
switch (kind)
{
// we can get here if someone asked about 1 variable declarator on multi local declaration
Debug.Assert(boundLocalDeclaration.Syntax.Kind() == SyntaxKind.VariableDeclarator);
return CreateVariableDeclaration(boundLocalDeclaration, boundLocalDeclaration.Syntax);
case SyntaxKind.LocalDeclarationStatement:
{
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)
{
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;
ITypeSymbol type = null;
Optional<object> constantValue = default(Optional<object>);
......
......@@ -51,7 +51,7 @@ internal IArgument CreateArgumentOperation(ArgumentKind kind, IParameterSymbol p
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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册