public static void SetAnnotations(List <VariableDeclarationStatementSyntax> annotations) { if (annotations == null || !_varValid) { return; } _activeVar.Annotations = new Dictionary <string, string>(); foreach (VariableDeclarationStatementSyntax vdsSyntax in annotations) { SeparatedSyntaxList <VariableDeclaratorSyntax> sSyntaxList = vdsSyntax.Declaration.Variables; if (sSyntaxList.Count != 1) { _varValid = false; return; } VariableDeclaratorSyntax vdSyntax = sSyntaxList[0]; string key = vdSyntax.Identifier.ValueText; EqualsValueClauseSyntax evcSyntax = (EqualsValueClauseSyntax)vdSyntax.Initializer; string value = ""; if (evcSyntax.Value is StringLiteralExpressionSyntax) { StringLiteralExpressionSyntax sleSyntax = (StringLiteralExpressionSyntax)evcSyntax.Value; List <SyntaxToken> tokens = sleSyntax.Tokens; if (tokens.Count > 0) { value = tokens[0].ValueText; } } else if (evcSyntax.Value is PrefixUnaryExpressionSyntax) { PrefixUnaryExpressionSyntax ueSyntax = (PrefixUnaryExpressionSyntax)evcSyntax.Value; value = ueSyntax.ToString(); } else if (evcSyntax.Value is LiteralExpressionSyntax) { LiteralExpressionSyntax ueSyntax = (LiteralExpressionSyntax)evcSyntax.Value; value = ueSyntax.ToString(); } else { throw new Exception("Unsupported annontation"); } _activeVar.Annotations.Add(key, value); } }
public override SyntaxNode VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node) { if (node.Kind() != SyntaxKind.UnaryMinusExpression) { return(base.VisitPrefixUnaryExpression(node)); } var expression = node.Operand; if (expression.Kind() != SyntaxKind.NumericLiteralExpression) { return(base.VisitPrefixUnaryExpression(node)); } CheckCastContex(node); string value = node.ToString(); bool found = false; VirtualData constant = null; foreach (var data in _virtualizationContext.data) { if (value.Equals(data.Name)) { found = true; constant = data; break; } } if (!found) { int index = _virtualizationContext.DataIndex; string name = value; SyntaxAnnotation indexMarker = new SyntaxAnnotation("index", index + ""); SyntaxAnnotation nameMarker = new SyntaxAnnotation("name", name); SyntaxAnnotation constantMarker = new SyntaxAnnotation("type", "constant"); SyntaxAnnotation codeMarker = new SyntaxAnnotation("code", "undefined"); SyntaxAnnotation uniqueMarker = new SyntaxAnnotation("unique", "" + VirtualizationContext.UniqueId); constant = new VirtualData(); constant.Index = index; constant.Name = name; var typeInfo = _virtualizationContext.semanticModel.GetTypeInfo(node); var info = typeInfo.Type.ToString(); constant.Type = info; constant.Node = node; constant.DefaultValue = node; constant.Annotations.Add(indexMarker); constant.Annotations.Add(nameMarker); constant.Annotations.Add(constantMarker); constant.Annotations.Add(codeMarker); constant.Annotations.Add(uniqueMarker); _virtualizationContext.data.Add(constant); } // constants.Add(node); ExpressionSyntax newNode = SyntaxFactoryExtensions.DataCodeVirtualAccess(); newNode = newNode.WithAdditionalAnnotations(constant.Annotations); ExpressionSyntax newExpression; if (CastEnabled) { newExpression = SyntaxFactory.CastExpression(SyntaxFactory.IdentifierName ( @"" + constant.Type ), newNode ); } else { newExpression = newNode; } //TODO: add annotations + comments newExpression = newExpression.WithLeadingTrivia(node.GetLeadingTrivia()) .WithTrailingTrivia(node.GetTrailingTrivia()) ; return(newExpression); }
public override TempComputeResult VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node) { return(VisitUnaryExpression(node.Operand, node.OperatorToken.Text, true, node.ToString())); }