public override void VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node) { if (node.Declaration.Variables.Count != 1) { // unexpected case throw new NotSupportedException(); } VariableDeclaratorSyntax variable = node.Declaration.Variables[0]; String variableId = variable.Identifier.Text; if (variableId != VariableName) { // unexpected case throw new NotSupportedException(); } IList <SyntaxNode> variableChildren = variable.ChildNodes().ToList(); if (variableChildren.Count == 1 && variableChildren[0] is EqualsValueClauseSyntax) { EqualsValueClauseSyntax value = (EqualsValueClauseSyntax)variableChildren[0]; FrameStack.Peek().States.Clear(); FrameStack.Peek().States.Add(new ExecutionState(Int32.Parse(value.Value.GetText().ToString()))); } base.VisitLocalDeclarationStatement(node); }
private void ProcessLocalDeclaration(LocalDeclarationStatementSyntax localDeclaration, Stack <ExecutionFrame> frameStack) { if (localDeclaration.Declaration.Variables.Count != 1) { // unexpected case throw new NotSupportedException(); } VariableDeclaratorSyntax variable = localDeclaration.Declaration.Variables[0]; String variableId = variable.Identifier.Text; if (variableId != VariableName) { // unexpected case throw new NotSupportedException(); } IList <SyntaxNode> variableChildren = variable.ChildNodes().ToList(); if (variableChildren.Count == 1 && variableChildren[0] is EqualsValueClauseSyntax) { EqualsValueClauseSyntax value = (EqualsValueClauseSyntax)variableChildren[0]; frameStack.Peek().States.Clear(); frameStack.Peek().States.Add(new ExecutionState(Int32.Parse(value.Value.GetText().ToString()))); } }
/// <summary> /// Gets the target of the provided <paramref name="expression"/> using the provided <paramref name="symbol"/> and <paramref name="model"/>. /// </summary> /// <param name="expression">An <see cref="InvocationExpressionSyntax"/> instance describing the invocation of a particular method.</param> /// <param name="symbol">An <see cref="IMethodSymbol"/> describing the method being invoked by the <paramref name="expression"/>.</param> /// <param name="model">A <see cref="SemanticModel"/> that can be used to resolve type information.</param> /// <param name="context">An <see cref="InvokedMethod"/> that represents the context in which to determine the target of the invocation.</param> /// <returns>Returns a <see cref="TypeInfo"/> instance representing the type of the target being invoked, or <c>null</c> if the type could not be determined.</returns> public static TypeInfo?GetTargetOfInvocation(this InvocationExpressionSyntax expression, IMethodSymbol symbol, SemanticModel model, InvokedMethod context) { TypeInfo?targetTypeInfo = expression.Expression.Kind() == SyntaxKind.IdentifierName ? context.TargetType : expression.GetTargetType(model, context); if (targetTypeInfo.HasValue) { MemberAccessExpressionSyntax memberAccess = expression.Expression as MemberAccessExpressionSyntax; IdentifierNameSyntax identifier = memberAccess?.Expression as IdentifierNameSyntax ?? expression.Expression as IdentifierNameSyntax; if (identifier != null) { SymbolInfo identifierSymbol = model.GetSymbolInfo(identifier); switch (identifierSymbol.Symbol.Kind) { case SymbolKind.Local: VariableDeclaratorSyntax syntax = identifierSymbol.Symbol.GetComparableSyntax()?.GetSyntax() as VariableDeclaratorSyntax; if (syntax != null && syntax.ChildNodes().Any()) { targetTypeInfo = model.GetTypeInfo(syntax.ChildNodes().First().ChildNodes().First()); } break; case SymbolKind.Parameter: if (context.Method != null) { var parameter = context.Method.Parameters.FirstOrDefault(p => p.Name == identifierSymbol.Symbol.Name); var parameterIndex = context.Method.Parameters.IndexOf(parameter); targetTypeInfo = context.ArgumentTypes.ElementAt(parameterIndex); } break; case SymbolKind.Field: SyntaxNode node = expression.Parent; while (!(node is BlockSyntax)) { node = node.Parent; } var statementsInBlock = node.ChildNodes().ToList(); var currentStatementIndex = statementsInBlock.IndexOf(expression.Parent); var assignments = statementsInBlock.Take(currentStatementIndex) .OfType <ExpressionStatementSyntax>() .Where(s => s.Expression.IsKind(SyntaxKind.SimpleAssignmentExpression)) .Select(s => s.Expression as AssignmentExpressionSyntax) .Where(s => model.GetSymbolInfo(s.Left).Symbol == identifierSymbol.Symbol); AssignmentExpressionSyntax lastAssignment = assignments.LastOrDefault(); if (lastAssignment != null) { targetTypeInfo = model.GetTypeInfo(lastAssignment.Right); } break; default: break; } } } return(targetTypeInfo); }
private Method TraverseVarDeclarators(VariableDeclaratorSyntax vds) { Method retMethod = new Method(); Variables retVar = new Variables(); if (vds.HasLeadingTrivia) { SetOuterComments(retVar, vds.GetLeadingTrivia().ToFullString()); } if (vds.HasTrailingTrivia) { SetInnerComments(retVar, vds.GetTrailingTrivia().ToFullString()); } retVar.Name = vds.Identifier.ValueText; Model.Type retType = new Model.Type(); retType.IsKnownType = SyntaxFacts.IsKeywordKind(vds.CSharpKind()); retType.IsNotUserDefined = SyntaxFacts.IsKeywordKind(vds.CSharpKind()); retVar.Type = retType; retMethod.AccessedVariables.Add(retVar); var valueClauses = from aValueClase in vds.ChildNodes().OfType<EqualsValueClauseSyntax>() select aValueClase; foreach (EqualsValueClauseSyntax evcs in valueClauses) { retMethod.AccessedVariables.AddRange(TraverseEqualsClause(evcs).AccessedVariables); } //TODO //Don't know if I need more stuff here //or even if i can fetch it from vds return retMethod; }