using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Semmle.Extraction.Kinds;
namespace Semmle.Extraction.CSharp.Entities.Expressions
{
internal class RecursivePattern : Expression
{
///
/// Creates and populates a recursive pattern.
///
/// The extraction context.
/// The syntax node of the recursive pattern.
/// The parent pattern/expression.
/// The child index of this pattern.
public RecursivePattern(Context cx, RecursivePatternSyntax syntax, IExpressionParentEntity parent, int child) :
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.RECURSIVE_PATTERN, parent, child, isCompilerGenerated: false, null))
{
// Extract the type access
if (syntax.Type is TypeSyntax t)
Expressions.TypeAccess.Create(cx, t, this, 1);
// Extract the local variable declaration
if (syntax.Designation is SingleVariableDesignationSyntax designation && cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
{
var type = symbol.GetAnnotatedType();
VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), false, this, 0);
}
if (syntax.PositionalPatternClause is PositionalPatternClauseSyntax posPc)
{
new PositionalPattern(cx, posPc, this, 2);
}
if (syntax.PropertyPatternClause is PropertyPatternClauseSyntax pc)
{
new PropertyPattern(cx, pc, this, 3);
}
}
}
}