Skip to content

Commit

Permalink
Shorten some unreasonably long lines of code.
Browse files Browse the repository at this point in the history
  • Loading branch information
gafter committed Mar 5, 2016
1 parent 3e87dff commit df879b5
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 37 deletions.
132 changes: 100 additions & 32 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,34 @@ private BoundExpression BindIsPatternExpression(IsPatternExpressionSyntax node,
var expression = BindExpression(node.Expression, diagnostics);
var hasErrors = IsOperandErrors(node, expression, diagnostics);
var pattern = BindPattern(node.Pattern, expression, expression.Type, hasErrors, diagnostics);
return new BoundIsPatternExpression(node, expression, pattern, GetSpecialType(SpecialType.System_Boolean, diagnostics, node), hasErrors);
return new BoundIsPatternExpression(
node, expression, pattern, GetSpecialType(SpecialType.System_Boolean, diagnostics, node), hasErrors);
}

internal BoundPattern BindPattern(PatternSyntax node, BoundExpression operand, TypeSymbol operandType, bool hasErrors, DiagnosticBag diagnostics)
internal BoundPattern BindPattern(
PatternSyntax node,
BoundExpression operand,
TypeSymbol operandType,
bool hasErrors,
DiagnosticBag diagnostics)
{
switch (node.Kind())
{
case SyntaxKind.DeclarationPattern:
return BindDeclarationPattern((DeclarationPatternSyntax)node, operand, operandType, hasErrors, diagnostics);
return BindDeclarationPattern(
(DeclarationPatternSyntax)node, operand, operandType, hasErrors, diagnostics);

case SyntaxKind.ConstantPattern:
return BindConstantPattern((ConstantPatternSyntax)node, operand, operandType, hasErrors, diagnostics);
return BindConstantPattern(
(ConstantPatternSyntax)node, operand, operandType, hasErrors, diagnostics);

case SyntaxKind.PropertyPattern:
return BindPropertyPattern((PropertyPatternSyntax)node, operand, operandType, hasErrors, diagnostics);
return BindPropertyPattern(
(PropertyPatternSyntax)node, operand, operandType, hasErrors, diagnostics);

case SyntaxKind.RecursivePattern:
return BindRecursivePattern((RecursivePatternSyntax)node, operand, operandType, hasErrors, diagnostics);
return BindRecursivePattern(
(RecursivePatternSyntax)node, operand, operandType, hasErrors, diagnostics);

case SyntaxKind.WildcardPattern:
return new BoundWildcardPattern(node);
Expand All @@ -44,7 +54,12 @@ internal BoundPattern BindPattern(PatternSyntax node, BoundExpression operand, T
}
}

private BoundPattern BindRecursivePattern(RecursivePatternSyntax node, BoundExpression operand, TypeSymbol operandType, bool hasErrors, DiagnosticBag diagnostics)
private BoundPattern BindRecursivePattern(
RecursivePatternSyntax node,
BoundExpression operand,
TypeSymbol operandType,
bool hasErrors,
DiagnosticBag diagnostics)
{
var type = (NamedTypeSymbol)this.BindType(node.Type, diagnostics);
hasErrors = hasErrors || CheckValidPatternType(node.Type, operand, operandType, type, false, diagnostics);
Expand Down Expand Up @@ -97,7 +112,8 @@ private BoundPattern BindRecursivePattern(RecursivePatternSyntax node, BoundExpr
if (!correspondingMembersForCtor.SequenceEqual(correspondingMembers, (s1, s2) => s1 == s2))
{
correspondingMembersForCtor.Free();
Error(diagnostics, ErrorCode.ERR_FeatureIsUnimplemented, node, "cannot infer a positional pattern from conflicting constructors");
Error(diagnostics, ErrorCode.ERR_FeatureIsUnimplemented, node,
"cannot infer a positional pattern from conflicting constructors");
diagnostics.Add(node, useSiteDiagnostics);
hasErrors = true;
return new BoundWildcardPattern(node, hasErrors);
Expand All @@ -108,7 +124,8 @@ private BoundPattern BindRecursivePattern(RecursivePatternSyntax node, BoundExpr

if (correspondingMembers == null)
{
Error(diagnostics, ErrorCode.ERR_FeatureIsUnimplemented, node, "cannot infer a positional pattern from any accessible constructor");
Error(diagnostics, ErrorCode.ERR_FeatureIsUnimplemented, node,
"cannot infer a positional pattern from any accessible constructor");
diagnostics.Add(node, useSiteDiagnostics);
correspondingMembersForCtor.Free();
hasErrors = true;
Expand All @@ -123,14 +140,28 @@ private BoundPattern BindRecursivePattern(RecursivePatternSyntax node, BoundExpr
var builder = ArrayBuilder<BoundSubPropertyPattern>.GetInstance(properties.Length);
for (int i = 0; i < properties.Length; i++)
{
var member = new BoundPropertyPatternMember(node.PatternList.SubPatterns[i], properties[i], ImmutableArray<BoundExpression>.Empty, ImmutableArray<string>.Empty, ImmutableArray<RefKind>.Empty, false, default(ImmutableArray<int>), LookupResultKind.Empty, properties[i].GetTypeOrReturnType(), hasErrors);
var member = new BoundPropertyPatternMember(
syntax: node.PatternList.SubPatterns[i],
memberSymbol: properties[i],
arguments: ImmutableArray<BoundExpression>.Empty,
argumentNamesOpt: ImmutableArray<string>.Empty,
argumentRefKindsOpt: ImmutableArray<RefKind>.Empty,
expanded: false,
argsToParamsOpt: default(ImmutableArray<int>),
resultKind: LookupResultKind.Empty,
type: properties[i].GetTypeOrReturnType(),
hasErrors: hasErrors);
builder.Add(new BoundSubPropertyPattern(node.PatternList.SubPatterns[i], member, boundPatterns[i], hasErrors));
}

return new BoundPropertyPattern(node, type, builder.ToImmutableAndFree(), hasErrors: hasErrors);
}

private ImmutableArray<BoundPattern> BindRecursiveSubPropertyPatterns(RecursivePatternSyntax node, ImmutableArray<Symbol> properties, NamedTypeSymbol type, DiagnosticBag diagnostics)
private ImmutableArray<BoundPattern> BindRecursiveSubPropertyPatterns(
RecursivePatternSyntax node,
ImmutableArray<Symbol> properties,
NamedTypeSymbol type,
DiagnosticBag diagnostics)
{
var boundPatternsBuilder = ArrayBuilder<BoundPattern>.GetInstance();
for (int i = 0; i < properties.Length; i++)
Expand All @@ -147,7 +178,10 @@ private ImmutableArray<BoundPattern> BindRecursiveSubPropertyPatterns(RecursiveP
return boundPatternsBuilder.ToImmutableAndFree();
}

private Symbol LookupMatchableMemberInType(TypeSymbol operandType, string name, ref HashSet<DiagnosticInfo> useSiteDiagnostics)
private Symbol LookupMatchableMemberInType(
TypeSymbol operandType,
string name,
ref HashSet<DiagnosticInfo> useSiteDiagnostics)
{
var lookupResult = LookupResult.GetInstance();
this.LookupMembersInType(
Expand All @@ -165,15 +199,23 @@ private Symbol LookupMatchableMemberInType(TypeSymbol operandType, string name,
return result;
}

private BoundPattern BindPropertyPattern(PropertyPatternSyntax node, BoundExpression operand, TypeSymbol operandType, bool hasErrors, DiagnosticBag diagnostics)
private BoundPattern BindPropertyPattern(
PropertyPatternSyntax node,
BoundExpression operand,
TypeSymbol operandType,
bool hasErrors,
DiagnosticBag diagnostics)
{
var type = (NamedTypeSymbol)this.BindType(node.Type, diagnostics);
hasErrors = hasErrors || CheckValidPatternType(node.Type, operand, operandType, type, false, diagnostics);
var boundPatterns = BindSubPropertyPatterns(node, type, diagnostics);
return new BoundPropertyPattern(node, type, boundPatterns, hasErrors: hasErrors);
}

private ImmutableArray<BoundSubPropertyPattern> BindSubPropertyPatterns(PropertyPatternSyntax node, TypeSymbol type, DiagnosticBag diagnostics)
private ImmutableArray<BoundSubPropertyPattern> BindSubPropertyPatterns(
PropertyPatternSyntax node,
TypeSymbol type,
DiagnosticBag diagnostics)
{
var result = ArrayBuilder<BoundSubPropertyPattern>.GetInstance();
foreach (var e in node.Expressions)
Expand All @@ -199,12 +241,6 @@ private BoundExpression BindPropertyPatternMember(

BoundImplicitReceiver implicitReceiver = new BoundImplicitReceiver(memberName.Parent, patternType);

// SPEC: Each member initializer must name an accessible field or property of the object being initialized, followed by an equals sign and
// SPEC: an expression or an object initializer or collection initializer.
// SPEC: A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (7.17.1) to the field or property.

// SPEC VIOLATION: Native compiler also allows initialization of field-like events in object initializers, so we allow it as well.

BoundExpression boundMember = BindInstanceMemberAccess(
node: memberName,
right: memberName,
Expand All @@ -221,7 +257,8 @@ private BoundExpression BindPropertyPatternMember(

if (boundMember.Kind == BoundKind.PropertyGroup)
{
boundMember = BindIndexedPropertyAccess((BoundPropertyGroup)boundMember, mustHaveAllOptionalParameters: true, diagnostics: diagnostics);
boundMember = BindIndexedPropertyAccess(
(BoundPropertyGroup)boundMember, mustHaveAllOptionalParameters: true, diagnostics: diagnostics);
if (boundMember.HasAnyErrors)
{
hasErrors = true;
Expand Down Expand Up @@ -314,11 +351,16 @@ private BoundExpression BadSubpatternMemberAccess(
return ToBadExpression(boundMember, LookupResultKind.NotAValue);
}

private Symbol FindPropertyOrFieldByName(TypeSymbol type, SyntaxToken name, out LookupResultKind resultKind, ref HashSet<DiagnosticInfo> useSiteDiagnostics)
private Symbol FindPropertyOrFieldByName(
TypeSymbol type,
SyntaxToken name,
out LookupResultKind resultKind,
ref HashSet<DiagnosticInfo> useSiteDiagnostics)
{
var symbols = ArrayBuilder<Symbol>.GetInstance();
var lookupResult = LookupResult.GetInstance();
this.LookupMembersWithFallback(lookupResult, type, name.ValueText, arity: 0, useSiteDiagnostics: ref useSiteDiagnostics);
this.LookupMembersWithFallback(
lookupResult, type, name.ValueText, arity: 0, useSiteDiagnostics: ref useSiteDiagnostics);
resultKind = lookupResult.Kind;
Symbol result = null;

Expand Down Expand Up @@ -346,7 +388,12 @@ private Symbol FindPropertyOrFieldByName(TypeSymbol type, SyntaxToken name, out
return result;
}

private BoundPattern BindConstantPattern(ConstantPatternSyntax node, BoundExpression operand, TypeSymbol operandType, bool hasErrors, DiagnosticBag diagnostics)
private BoundPattern BindConstantPattern(
ConstantPatternSyntax node,
BoundExpression operand,
TypeSymbol operandType,
bool hasErrors,
DiagnosticBag diagnostics)
{
var expression = BindValue(node.Expression, diagnostics, BindValueKind.RValue);
if (!node.HasErrors && expression.ConstantValue == null)
Expand All @@ -359,7 +406,13 @@ private BoundPattern BindConstantPattern(ConstantPatternSyntax node, BoundExpres
return new BoundConstantPattern(node, expression, hasErrors);
}

private bool CheckValidPatternType(CSharpSyntaxNode typeSyntax, BoundExpression operand, TypeSymbol operandType, TypeSymbol patternType, bool isVar, DiagnosticBag diagnostics)
private bool CheckValidPatternType(
CSharpSyntaxNode typeSyntax,
BoundExpression operand,
TypeSymbol operandType,
TypeSymbol patternType,
bool isVar,
DiagnosticBag diagnostics)
{
if (operandType?.IsErrorType() == true || patternType?.IsErrorType() == true)
{
Expand Down Expand Up @@ -409,7 +462,11 @@ private bool CheckValidPatternType(CSharpSyntaxNode typeSyntax, BoundExpression
}

private BoundPattern BindDeclarationPattern(
DeclarationPatternSyntax node, BoundExpression operand, TypeSymbol operandType, bool hasErrors, DiagnosticBag diagnostics)
DeclarationPatternSyntax node,
BoundExpression operand,
TypeSymbol operandType,
bool hasErrors,
DiagnosticBag diagnostics)
{
Debug.Assert(operand != null || (object)operandType != null);
var typeSyntax = node.Type;
Expand Down Expand Up @@ -462,7 +519,10 @@ private BoundPattern BindDeclarationPattern(
return new BoundDeclarationPattern(node, localSymbol, boundDeclType, isVar, hasErrors);
}

private TypeSymbol BestType(MatchExpressionSyntax node, ArrayBuilder<BoundMatchCase> cases, DiagnosticBag diagnostics)
private TypeSymbol BestType(
MatchExpressionSyntax node,
ArrayBuilder<BoundMatchCase> cases,
DiagnosticBag diagnostics)
{
var types = ArrayBuilder<TypeSymbol>.GetInstance();

Expand Down Expand Up @@ -514,7 +574,9 @@ private TypeSymbol BestType(MatchExpressionSyntax node, ArrayBuilder<BoundMatchC
return bestType;
}

private BoundExpression BindMatchExpression(MatchExpressionSyntax node, DiagnosticBag diagnostics)
private BoundExpression BindMatchExpression(
MatchExpressionSyntax node,
DiagnosticBag diagnostics)
{
var expression = BindValue(node.Left, diagnostics, BindValueKind.RValue);
// TODO: any constraints on a switch expression must be enforced here. For example,
Expand Down Expand Up @@ -591,7 +653,12 @@ private BoundStatement BindLetStatement(LetStatementSyntax node, DiagnosticBag d
}
if (hasErrors && expression.Type == (object)null)
{
expression = new BoundBadExpression(node.Expression, LookupResultKind.Viable, ImmutableArray<Symbol>.Empty, ImmutableArray.Create<BoundNode>(expression), CreateErrorType());
expression = new BoundBadExpression(
syntax: node.Expression,
resultKind: LookupResultKind.Viable,
symbols: ImmutableArray<Symbol>.Empty,
childBoundNodes: ImmutableArray.Create<BoundNode>(expression),
type: CreateErrorType());
}

BoundPattern pattern;
Expand All @@ -615,9 +682,10 @@ private BoundStatement BindLetStatement(LetStatementSyntax node, DiagnosticBag d

localSymbol.SetTypeSymbol(expression.Type);

pattern = new BoundDeclarationPattern(node, localSymbol, null, true,
expression.HasErrors |
this.ValidateDeclarationNameConflictsInScope(localSymbol, diagnostics)); // Check for variable declaration errors.
pattern = new BoundDeclarationPattern(
node, localSymbol, null, true,
// Check for variable declaration errors.
expression.HasErrors | this.ValidateDeclarationNameConflictsInScope(localSymbol, diagnostics));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ BoundExpression TranslatePattern(BoundExpression input, BoundPattern pattern)
var subProperty = (pat.Subpatterns[i].Member as BoundPropertyPatternMember)?.MemberSymbol;
var subPattern = pat.Subpatterns[i].Pattern;
var subExpression =
subProperty?.Kind == SymbolKind.Field ? _factory.Field(input, (FieldSymbol)subProperty) :
subProperty?.Kind == SymbolKind.Property ? _factory.Call(input, ((PropertySymbol)subProperty).GetMethod) :
(BoundExpression)new BoundBadExpression(_factory.Syntax, LookupResultKind.Inaccessible, ImmutableArray<Symbol>.Empty, ImmutableArray<BoundNode>.Empty, pat.Type);
subProperty?.Kind == SymbolKind.Field ?
_factory.Field(input, (FieldSymbol)subProperty) :
subProperty?.Kind == SymbolKind.Property ?
_factory.Call(input, ((PropertySymbol)subProperty).GetMethod) :
(BoundExpression)new BoundBadExpression(
_factory.Syntax, LookupResultKind.Inaccessible, ImmutableArray<Symbol>.Empty,
ImmutableArray<BoundNode>.Empty, pat.Type);
var partialMatch = this.TranslatePattern(subExpression, subPattern);
matched = _factory.LogicalAnd(matched, partialMatch);
}
Expand Down Expand Up @@ -128,7 +132,9 @@ BoundExpression DeclPattern(CSharpSyntaxNode syntax, BoundExpression input, Loca
var tmpType = _factory.SpecialType(SpecialType.System_Nullable_T).Construct(type);
var tmp = _factory.SynthesizedLocal(tmpType, syntax);
var asg1 = _factory.AssignmentExpression(_factory.Local(tmp), _factory.As(input, tmpType));
var value = _factory.Call(_factory.Local(tmp), GetNullableMethod(syntax, tmpType, SpecialMember.System_Nullable_T_GetValueOrDefault));
var value = _factory.Call(
_factory.Local(tmp),
GetNullableMethod(syntax, tmpType, SpecialMember.System_Nullable_T_GetValueOrDefault));
var asg2 = _factory.AssignmentExpression(_factory.Local(target), value);
var result = MakeNullableHasValue(syntax, _factory.Local(tmp));
return _factory.Sequence(tmp, asg1, asg2, result);
Expand All @@ -143,7 +149,10 @@ BoundExpression DeclPattern(CSharpSyntaxNode syntax, BoundExpression input, Loca
// return s;
// }
return _factory.Conditional(_factory.Is(input, type),
_factory.Sequence(_factory.AssignmentExpression(_factory.Local(target), _factory.Convert(type, input)), _factory.Literal(true)),
_factory.Sequence(_factory.AssignmentExpression(
_factory.Local(target),
_factory.Convert(type, input)),
_factory.Literal(true)),
_factory.Literal(false),
_factory.SpecialType(SpecialType.System_Boolean));
}
Expand Down

0 comments on commit df879b5

Please sign in to comment.