private bool IsEquivalent(ExpressionSyntax expression, ParameterSyntax parameter) { var identifier = expression.As <IdentifierNameSyntax>()?.Identifier; var paramIdentifier = parameter.Identifier; return(identifier?.ValueText == paramIdentifier.ValueText); }
private string ReplaceGenericVar(string rawString, ExpressionSyntax expression) { var name = expression.As <MemberAccessExpressionSyntax>().Name.As <GenericNameSyntax>(); var genericVar = TypeProcessor.ConvertType(name.TypeArgumentList.Arguments.Single()); return(rawString.Replace("{genericType}", genericVar)); }
private ExpressionSyntax GetSourceName(ExpressionSyntax assigneeExpression) { string assignee = assigneeExpression .As <IdentifierNameSyntax>() ?.Identifier.Text; string assigneeOfThis = assigneeExpression .As <MemberAccessExpressionSyntax>() .Unless(m => m?.Expression is ThisExpressionSyntax) ?.Name?.Identifier.Text; assignee = assignee ?? assigneeOfThis; if (assignee == null) { return(null); } return("argument".ToIdentifierName().AccessTo(assignee)); }
private string GetBuilderSetterMethod(ExpressionSyntax assigment) { var nodes = assigment.DescendantNodes <IdentifierNameSyntax>(); if (assigment is IdentifierNameSyntax) { nodes = nodes.Prepend(assigment.As <IdentifierNameSyntax>()); } var parameter = nodes .Select(p => p.Identifier.Text) .Select(p => _parameters.FirstOrDefault(q => q.Identifier.Text == p)) .FirstOrDefault(p => p != null); return(GetAssigmentMethodName(parameter)); }
private string ReplaceGenericVar(string rawString, ExpressionSyntax expression) { var name = expression.As <MemberAccessExpressionSyntax>().Name; if (name is GenericNameSyntax) { var genericVar = TypeProcessor.ConvertType(name.As <GenericNameSyntax>().TypeArgumentList.Arguments.Single()); return(rawString.Replace("{genericType}", genericVar)); } else { var typeArg = Program.GetModel(expression).GetSymbolInfo(expression).Symbol.As <IMethodSymbol>().TypeArguments.Single(); return(rawString.Replace("{genericType}", TypeProcessor.ConvertType(typeArg))); } }
public static ITypeSymbol GetTypeSymbol(ExpressionSyntax node, SemanticModel semanticModel) { Contract.Requires(node != null); Contract.Requires(semanticModel != null); ITypeSymbol typeSymbol; if (node.As <IdentifierNameSyntax>()?.Identifier.Text == "var") { node = node.Parent.As <VariableDeclarationSyntax>() ?.Variables.FirstOrDefault() ?.Initializer?.Value; typeSymbol = semanticModel.GetSpeculativeExpressionType(node); } else { typeSymbol = semanticModel.GetSpeculativeTypeSymbol(node as TypeSyntax); } return(typeSymbol); }
private static ExpressionSyntax RemoveLogicalNot(ExpressionSyntax expression) { if (expression is ParenthesizedExpressionSyntax) { return(RemoveLogicalNot(expression.As <ParenthesizedExpressionSyntax>()?.Expression)?.ToParenthesized()); } var unaryExpression = expression as PrefixUnaryExpressionSyntax; if (unaryExpression?.IsKind(LogicalNotExpression) ?? false) { return(unaryExpression.Operand); } var oppositeBinaryExpression = ToOpposite(expression as BinaryExpressionSyntax); if (oppositeBinaryExpression != null) { return(oppositeBinaryExpression); } return(null); }
public static bool AccessToComponentIsFinishedProperty(this ExpressionSyntax expr) => expr is MemberAccessExpressionSyntax && expr.As <MemberAccessExpressionSyntax>().Name.Identifier.ValueText == "IsFinished" && expr.As <MemberAccessExpressionSyntax>().Expression.As <MemberAccessExpressionSyntax>()?.Name.Identifier.ValueText == "Component";
private static void Go(ScalaWriter writer, ExpressionSyntax left, SyntaxToken operatorToken, ExpressionSyntax right) { if (operatorToken.Kind() == SyntaxKind.AsKeyword) { writer.Write("CsScala.As["); writer.Write(TypeProcessor.ConvertType(right)); writer.Write("]("); Core.Write(writer, left); writer.Write(")"); } else if (operatorToken.Kind() == SyntaxKind.IsKeyword) { Core.Write(writer, left); writer.Write(".isInstanceOf["); writer.Write(TypeProcessor.ConvertType(right)); writer.Write("]"); } else if (operatorToken.Kind() == SyntaxKind.QuestionQuestionToken) { writer.Write("CsScala.Coalesce("); Core.Write(writer, left); writer.Write(", "); Core.Write(writer, right); writer.Write(")"); } else { if (left is ElementAccessExpressionSyntax && IsAssignmentToken(operatorToken.Kind())) { var subExpr = left.As <ElementAccessExpressionSyntax>(); var typeStr = TypeProcessor.GenericTypeName(Program.GetModel(left).GetTypeInfo(subExpr.Expression).Type); var trans = ElementAccessTranslation.Get(typeStr); if (trans != null) { Core.Write(writer, subExpr.Expression); writer.Write("."); if (operatorToken.Kind() == SyntaxKind.EqualsToken) { writer.Write(trans.ReplaceAssign); } else { throw new Exception(operatorToken.Kind() + " is not supported on " + typeStr + " " + Utility.Descriptor(left.Parent)); } writer.Write("("); foreach (var arg in subExpr.ArgumentList.Arguments) { Core.Write(writer, arg.Expression); writer.Write(", "); } Core.Write(writer, right); writer.Write(")"); return; } } Action <ExpressionSyntax> write = e => { var model = Program.GetModel(left); var type = model.GetTypeInfo(e); //Check for enums being converted to strings by string concatenation if (operatorToken.Kind() == SyntaxKind.PlusToken && type.Type.TypeKind == TypeKind.Enum) { writer.Write(type.Type.ContainingNamespace.FullNameWithDot()); writer.Write(WriteType.TypeName(type.Type.As <INamedTypeSymbol>())); writer.Write(".ToString("); Core.Write(writer, e); writer.Write(")"); } else if (operatorToken.Kind() == SyntaxKind.PlusToken && (type.Type.Name == "Nullable" && type.Type.ContainingNamespace.FullName() == "System" && type.Type.As <INamedTypeSymbol>().TypeArguments.Single().TypeKind == TypeKind.Enum)) { var enumType = type.Type.As <INamedTypeSymbol>().TypeArguments.Single(); writer.Write(enumType.ContainingNamespace.FullNameWithDot()); writer.Write(WriteType.TypeName(enumType.As <INamedTypeSymbol>())); writer.Write(".ToString("); Core.Write(writer, e); writer.Write(")"); } else if (operatorToken.Kind() == SyntaxKind.PlusToken && IsException(type.Type)) //Check for exceptions being converted to strings by string concatenation { writer.Write("System.CsScala.ExceptionToString("); Core.Write(writer, e); writer.Write(")"); } else if (operatorToken.Kind() == SyntaxKind.PlusToken && type.Type.SpecialType == SpecialType.System_Byte && !Utility.IsNumeric(type.ConvertedType)) { //bytes are signed in the JVM, so we need to take care when converting them to strings. Exclude numeric types, since Core.Writer will convert these to ints writer.Write("System.CsScala.ByteToString("); Core.Write(writer, e); writer.Write(")"); } else if (operatorToken.Kind() == SyntaxKind.PlusToken && !(e is BinaryExpressionSyntax) && type.Type.SpecialType == SpecialType.System_String && CouldBeNullString(model, e)) { //In .net, concatenating a null string does not alter the output. However, in the JVM, it produces the "null" string. To counter this, we must check non-const strings. writer.Write("System.CsScala.NullCheck("); Core.Write(writer, e); writer.Write(")"); } else if (operatorToken.Kind() == SyntaxKind.PlusToken && !(e is BinaryExpressionSyntax) && type.Type is INamedTypeSymbol && type.Type.As <INamedTypeSymbol>().ConstructedFrom.SpecialType == SpecialType.System_Nullable_T) { //Concatening a nullable type in .net just produces an empty string if it's null. In scala it produces "null" or a null reference exception -- we want neither. writer.Write("System.CsScala.NullCheck("); Core.Write(writer, e); writer.Write(")"); } else if (operatorToken.Kind() == SyntaxKind.PlusToken && !(e is BinaryExpressionSyntax) && type.Type.SpecialType == SpecialType.System_Boolean) { writer.Write("System.CsScala.BooleanToString("); Core.Write(writer, e); writer.Write(")"); } else { Core.Write(writer, e); } }; write(left); writer.Write(" "); writer.Write(operatorToken.ToString()); writer.Write(" "); write(right); } }