Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOperation for recursive pattern and switch expression #31967

Merged
merged 8 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Patterns.playlist

This file was deleted.

54 changes: 34 additions & 20 deletions src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,8 @@ private IConstantPatternOperation CreateBoundConstantPatternOperation(BoundConst
BoundNode value = boundConstantPattern.Value;
SyntaxNode syntax = boundConstantPattern.Syntax;
bool isImplicit = boundConstantPattern.WasCompilerGenerated;
return new CSharpLazyConstantPatternOperation(this, value, _semanticModel, syntax, isImplicit);
TypeSymbol inputType = boundConstantPattern.InputType;
return new CSharpLazyConstantPatternOperation(inputType, this, value, _semanticModel, syntax, isImplicit);
}

private IDeclarationPatternOperation CreateBoundDeclarationPatternOperation(BoundDeclarationPattern boundDeclarationPattern)
Expand All @@ -1837,21 +1838,30 @@ private IDeclarationPatternOperation CreateBoundDeclarationPatternOperation(Boun
variable = ((BoundDiscardExpression)boundDeclarationPattern.VariableAccess).ExpressionSymbol;
}

ITypeSymbol inputType = boundDeclarationPattern.InputType;
bool acceptsNull = boundDeclarationPattern.IsVar;
Copy link
Member

@jcouv jcouv Dec 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

acceptsNull [](start = 17, length = 11)

What about x is null? If the constant is null, then we should probably set AcceptsNull to true.
If the intention is to capture IsVar, then maybe the pattern should have an IsVar property instead.
As Fred pointed out in a test, should AcceptsNull be true when matching types that can't be null in the first place (like int)? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the declaration pattern only.
The spec for a var pattern is that it accepts null. The fact that it will never get null in some circumstances doesn't affect that fact.


In reply to: 244032712 [](ancestors = 244032712)

ITypeSymbol matchedType = acceptsNull ? null : boundDeclarationPattern.DeclaredType.Type;
SyntaxNode syntax = boundDeclarationPattern.Syntax;
ITypeSymbol type = null;
Optional<object> constantValue = default(Optional<object>);
bool isImplicit = boundDeclarationPattern.WasCompilerGenerated;
return new DeclarationPatternOperation(variable, _semanticModel, syntax, type, constantValue, isImplicit);
return new DeclarationPatternOperation(inputType, matchedType, variable, acceptsNull, _semanticModel, syntax, isImplicit);
}

private IRecursivePatternOperation CreateBoundRecursivePatternOperation(BoundRecursivePattern boundRecursivePattern)
{
bool isImplicit = boundRecursivePattern.WasCompilerGenerated;
ITypeSymbol matchedType = boundRecursivePattern.DeclaredType?.Type ?? boundRecursivePattern.InputType;
ISymbol variable = boundRecursivePattern.Variable;
ISymbol deconstructSymbol = boundRecursivePattern.DeconstructMethod;
if (variable == null && boundRecursivePattern.VariableAccess?.Kind == BoundKind.DiscardExpression)
{
variable = ((BoundDiscardExpression)boundRecursivePattern.VariableAccess).ExpressionSymbol;
}

TypeSymbol inputType = boundRecursivePattern.InputType;
SyntaxNode syntax = boundRecursivePattern.Syntax;
ITypeSymbol type = null;
Optional<object> constantValue = default(Optional<object>);
bool isImplicit = boundRecursivePattern.WasCompilerGenerated;
return new RecursivePattern(variable, _semanticModel, syntax, type, constantValue, isImplicit);
return new CSharpLazyRecursivePatternOperation(
this, inputType, matchedType, deconstructSymbol, boundRecursivePattern.Deconstruction,
boundRecursivePattern.Properties, variable, _semanticModel, syntax, isImplicit);
}

private ISwitchOperation CreateBoundSwitchStatementOperation(BoundSwitchStatement boundSwitchStatement)
Expand Down Expand Up @@ -1931,12 +1941,14 @@ private IOperation CreateBoundRangeVariableOperation(BoundRangeVariable boundRan

private IOperation CreateBoundDiscardExpressionOperation(BoundDiscardExpression boundNode)
{
return new DiscardOperation((IDiscardSymbol)boundNode.ExpressionSymbol,
_semanticModel,
boundNode.Syntax,
boundNode.Type,
ConvertToOptional(boundNode.ConstantValue),
isImplicit: boundNode.WasCompilerGenerated);
return new DiscardOperation(
inputType: null,
(IDiscardSymbol)boundNode.ExpressionSymbol,
_semanticModel,
boundNode.Syntax,
boundNode.Type,
ConvertToOptional(boundNode.ConstantValue),
isImplicit: boundNode.WasCompilerGenerated);
}

private IOperation CreateFromEndIndexExpressionOperation(BoundFromEndIndexExpression boundIndex)
Expand Down Expand Up @@ -1967,12 +1979,14 @@ private IOperation CreateRangeExpressionOperation(BoundRangeExpression boundRang

private IOperation CreateBoundDiscardPatternOperation(BoundDiscardPattern boundNode)
{
return new DiscardOperation(boundNode.DiscardSymbol,
_semanticModel,
boundNode.Syntax,
boundNode.InputType,
null,
isImplicit: boundNode.WasCompilerGenerated);
return new DiscardOperation(
boundNode.InputType,
boundNode.DiscardSymbol,
_semanticModel,
boundNode.Syntax,
boundNode.InputType,
null,
isImplicit: boundNode.WasCompilerGenerated);
}
}
}
40 changes: 38 additions & 2 deletions src/Compilers/CSharp/Portable/Operations/CSharpOperationNodes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -1469,8 +1470,8 @@ internal sealed class CSharpLazyConstantPatternOperation : LazyConstantPatternOp
private readonly CSharpOperationFactory _operationFactory;
private readonly BoundNode _value;

internal CSharpLazyConstantPatternOperation(CSharpOperationFactory operationFactory, BoundNode value, SemanticModel semanticModel, SyntaxNode syntax, bool isImplicit) :
base(semanticModel, syntax, isImplicit)
internal CSharpLazyConstantPatternOperation(ITypeSymbol inputType, CSharpOperationFactory operationFactory, BoundNode value, SemanticModel semanticModel, SyntaxNode syntax, bool isImplicit) :
base(inputType, semanticModel, syntax, isImplicit)
{
_operationFactory = operationFactory;
_value = value;
Expand All @@ -1482,6 +1483,41 @@ protected override IOperation CreateValue()
}
}

/// <summary>
/// Represents a C# recursive pattern.
/// </summary>
internal sealed partial class CSharpLazyRecursivePatternOperation : LazyRecursivePatternOperation
{
private readonly CSharpOperationFactory _operationFactory;
private readonly ImmutableArray<BoundSubpattern> _deconstructionSubpatterns;
private readonly ImmutableArray<BoundSubpattern> _propertySubpatterns;
public CSharpLazyRecursivePatternOperation(
CSharpOperationFactory operationFactory,
ITypeSymbol inputType,
ITypeSymbol matchedType,
ISymbol deconstructSymbol,
ImmutableArray<BoundSubpattern> deconstructionSubpatterns,
ImmutableArray<BoundSubpattern> propertySubpatterns,
ISymbol declaredSymbol,
SemanticModel semanticModel,
SyntaxNode syntax,
bool isImplicit)
: base(inputType, matchedType, deconstructSymbol, declaredSymbol, semanticModel, syntax, isImplicit)
{
_operationFactory = operationFactory;
_deconstructionSubpatterns = deconstructionSubpatterns;
_propertySubpatterns = propertySubpatterns;
}
public override ImmutableArray<IPatternOperation> CreateDeconstructionSubpatterns()
{
return _deconstructionSubpatterns.SelectAsArray(p => (IPatternOperation)_operationFactory.Create(p.Pattern));
Copy link
Member

@jcouv jcouv Dec 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p [](start = 60, length = 1)

nit: p should be d #ByDesign

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is a subpattern.


In reply to: 244034917 [](ancestors = 244034917)

}
public override ImmutableArray<(ISymbol, IPatternOperation)> CreatePropertySubpatterns()
{
return _propertySubpatterns.SelectAsArray(p => ((ISymbol)p.Symbol, (IPatternOperation)_operationFactory.Create(p.Pattern)));
}
}

internal sealed class CSharpLazyPatternCaseClauseOperation : LazyPatternCaseClauseOperation
{
private readonly CSharpOperationFactory _operationFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ void M(object o)
Expression:
IParameterReferenceOperation: o (OperationKind.ParameterReference, Type: System.Object) (Syntax: 'o')
Pattern:
IDeclarationPatternOperation (Declared Symbol: System.Int32 x) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'int x')
IDeclarationPatternOperation (Declared Symbol: System.Int32 x, AcceptsNull: False) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'int x')
Collection:
ILocalReferenceOperation: arr (OperationKind.LocalReference, Type: System.Int32[]) (Syntax: 'arr')
Body:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ private static void A(bool flag, int number)
Expression:
ILocalReferenceOperation: o (OperationKind.LocalReference, Type: System.Object) (Syntax: 'o')
Pattern:
IDeclarationPatternOperation (Declared Symbol: System.Int32 i) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'int i')
IDeclarationPatternOperation (Declared Symbol: System.Int32 i, AcceptsNull: False) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'int i')
InConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
OutConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
IArgumentOperation (ArgumentKind.Explicit, Matching Parameter: number) (OperationKind.Argument, Type: null) (Syntax: '1')
Expand Down Expand Up @@ -743,7 +743,7 @@ private void M()
Expression:
ILocalReferenceOperation: obj (OperationKind.LocalReference, Type: System.Object) (Syntax: 'obj')
Pattern:
IDeclarationPatternOperation (Declared Symbol: System.String str) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'string str')
IDeclarationPatternOperation (Declared Symbol: System.String str, AcceptsNull: False) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'string str')
WhenTrue:
IBlockOperation (1 statements) (OperationKind.Block, Type: null) (Syntax: '{ ... }')
IExpressionStatementOperation (OperationKind.ExpressionStatement, Type: null) (Syntax: 'Console.WriteLine(str);')
Expand Down Expand Up @@ -854,7 +854,7 @@ private static void A(bool flag, int number)
Expression:
ILocalReferenceOperation: o (OperationKind.LocalReference, Type: System.Object) (Syntax: 'o')
Pattern:
IDeclarationPatternOperation (Declared Symbol: System.Int32 i) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'int i')
IDeclarationPatternOperation (Declared Symbol: System.Int32 i, AcceptsNull: False) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'int i')
InConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
OutConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
IArgumentOperation (ArgumentKind.Explicit, Matching Parameter: number) (OperationKind.Argument, Type: null) (Syntax: '1')
Expand Down Expand Up @@ -1250,7 +1250,7 @@ private void M()
Expression:
ILocalReferenceOperation: obj (OperationKind.LocalReference, Type: System.Object) (Syntax: 'obj')
Pattern:
IDeclarationPatternOperation (Declared Symbol: System.String str) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'string str')
IDeclarationPatternOperation (Declared Symbol: System.String str, AcceptsNull: False) (OperationKind.DeclarationPattern, Type: null) (Syntax: 'string str')
WhenTrue:
IBlockOperation (1 statements) (OperationKind.Block, Type: null) (Syntax: '{ ... }')
IExpressionStatementOperation (OperationKind.ExpressionStatement, Type: null) (Syntax: 'Console.WriteLine(str);')
Expand Down
Loading