public void CSharpNullableLocalVariableDeclarationTest4() { LocalVariableDeclaration lvd = ParseUtilCSharp.ParseStatement <LocalVariableDeclaration>("SomeStruct<int?>? a;"); Assert.AreEqual(1, lvd.Variables.Count); Assert.AreEqual("a", ((VariableDeclaration)lvd.Variables[0]).Name); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("System.Nullable", type.Type); Assert.AreEqual("SomeStruct", type.GenericTypes[0].Type); Assert.AreEqual("System.Nullable", type.GenericTypes[0].GenericTypes[0].Type); Assert.AreEqual("System.Int32", type.GenericTypes[0].GenericTypes[0].GenericTypes[0].Type); }
public void CSharpGenericLocalVariableDeclarationTest2WithoutSpace() { LocalVariableDeclaration lvd = ParseUtilCSharp.ParseStatement <LocalVariableDeclaration>("G<G<int>> a;"); Assert.AreEqual(1, lvd.Variables.Count); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("G", type.Type); Assert.AreEqual(1, type.GenericTypes.Count); Assert.AreEqual("G", type.GenericTypes[0].Type); Assert.AreEqual(1, type.GenericTypes[0].GenericTypes.Count); Assert.AreEqual("System.Int32", type.GenericTypes[0].GenericTypes[0].Type); }
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data) { for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) { VariableDeclaration varDecl = (VariableDeclaration)localVariableDeclaration.Variables[i]; AddVariable(localVariableDeclaration.GetTypeForVariable(i), varDecl.Name, localVariableDeclaration.StartLocation, CurrentEndLocation, (localVariableDeclaration.Modifier & Modifiers.Const) == Modifiers.Const); } return(base.VisitLocalVariableDeclaration(localVariableDeclaration, data)); }
public override object Visit(LocalVariableDeclaration localVariableDeclaration, object data) { for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) { VariableDeclaration varDecl = (VariableDeclaration)localVariableDeclaration.Variables[i]; AddVariable(localVariableDeclaration.GetTypeForVariable(i), varDecl.Name, localVariableDeclaration.StartLocation, CurrentBlock == null ? new Point(-1, -1) : CurrentBlock.EndLocation, (localVariableDeclaration.Modifier & Modifier.Const) == Modifier.Const); } return(data); }
public void VBNetGenericWithArrayLocalVariableDeclarationTest1() { LocalVariableDeclaration lvd = ParseUtilVBNet.ParseStatement <LocalVariableDeclaration>("Dim a As G(Of Integer)()"); Assert.AreEqual(1, lvd.Variables.Count); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("G", type.Type); Assert.AreEqual(1, type.GenericTypes.Count); Assert.AreEqual("Integer", type.GenericTypes[0].Type); Assert.AreEqual(0, type.GenericTypes[0].GenericTypes.Count); Assert.IsFalse(type.GenericTypes[0].IsArrayType); Assert.AreEqual(new int[] { 0 }, type.RankSpecifier); }
public void CSharpGenericWithArrayLocalVariableDeclarationTest1() { LocalVariableDeclaration lvd = ParseUtilCSharp.ParseStatement <LocalVariableDeclaration>("G<int>[] a;"); Assert.AreEqual(1, lvd.Variables.Count); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("G", type.Type); Assert.AreEqual(1, type.GenericTypes.Count); Assert.AreEqual("System.Int32", type.GenericTypes[0].Type); Assert.AreEqual(0, type.GenericTypes[0].GenericTypes.Count); Assert.IsFalse(type.GenericTypes[0].IsArrayType); Assert.AreEqual(new int[] { 0 }, type.RankSpecifier); }
public void VBNetGenericWithArrayLocalVariableDeclarationTest2() { LocalVariableDeclaration lvd = ParseUtilVBNet.ParseStatement <LocalVariableDeclaration>("Dim a As G(Of Integer())"); Assert.AreEqual(1, lvd.Variables.Count); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("G", type.Type); Assert.AreEqual(1, type.GenericTypes.Count); Assert.AreEqual("System.Int32", type.GenericTypes[0].Type); Assert.AreEqual(0, type.GenericTypes[0].GenericTypes.Count); Assert.IsFalse(type.IsArrayType); Assert.AreEqual(1, type.GenericTypes[0].RankSpecifier.Length); Assert.AreEqual(0, type.GenericTypes[0].RankSpecifier[0]); }
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration lvd, object data) { for (int i = 0; i < lvd.Variables.Count; ++i) { VariableDeclaration varDecl = (VariableDeclaration)lvd.Variables[i]; AddVariable(lvd.GetTypeForVariable(i), varDecl.Name, lvd.StartLocation, CurrentEndLocation, (lvd.Modifier & Modifiers.Const) == Modifiers.Const, false, varDecl.Initializer, null); } return(base.VisitLocalVariableDeclaration(lvd, data)); }
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data) { if (AddDefaultValueInitializerToLocalVariableDeclarations) { for (int i = 0; i < localVariableDeclaration.Variables.Count; i++) { VariableDeclaration decl = localVariableDeclaration.Variables[i]; if (decl.FixedArrayInitialization.IsNull && decl.Initializer.IsNull) { TypeReference type = localVariableDeclaration.GetTypeForVariable(i); decl.Initializer = ExpressionBuilder.CreateDefaultValueForType(type); } } } return(base.VisitLocalVariableDeclaration(localVariableDeclaration, data)); }
public void VBNetComplexGenericLocalVariableDeclarationTest() { LocalVariableDeclaration lvd = ParseUtilVBNet.ParseStatement <LocalVariableDeclaration>("Dim where As Generic(Of Printable, G(Of Printable()))"); Assert.AreEqual(1, lvd.Variables.Count); Assert.AreEqual("where", lvd.Variables[0].Name); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("Generic", type.Type); Assert.AreEqual(2, type.GenericTypes.Count); Assert.AreEqual("Printable", type.GenericTypes[0].Type); Assert.AreEqual(0, type.GenericTypes[0].GenericTypes.Count); Assert.AreEqual("G", type.GenericTypes[1].Type); Assert.AreEqual(1, type.GenericTypes[1].GenericTypes.Count); Assert.AreEqual("Printable", type.GenericTypes[1].GenericTypes[0].Type); }
public void VBNetLocalJaggedArrayDeclarationTest() { LocalVariableDeclaration lvd = ParseUtilVBNet.ParseStatement <LocalVariableDeclaration>("Dim a(10)() As Integer"); Assert.AreEqual(1, lvd.Variables.Count); Assert.AreEqual("a", lvd.Variables[0].Name); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("System.Int32", type.Type); Assert.AreEqual(new int[] { 0, 0 }, type.RankSpecifier); ArrayCreateExpression ace = (ArrayCreateExpression)lvd.Variables[0].Initializer; Assert.AreEqual(new int[] { 0, 0 }, ace.CreateType.RankSpecifier); Assert.AreEqual(1, ace.Arguments.Count); Assert.AreEqual(10, ((PrimitiveExpression)ace.Arguments[0]).Value); }
public void VBNetLocalArrayDeclarationWithInitializationAndLowerBoundTest() { // VB.NET allows only "0" as lower bound LocalVariableDeclaration lvd = ParseUtilVBNet.ParseStatement <LocalVariableDeclaration>("Dim a(0 To 10) As Integer"); Assert.AreEqual(1, lvd.Variables.Count); Assert.AreEqual("a", lvd.Variables[0].Name); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("System.Int32", type.Type); Assert.AreEqual(new int[] { 0 }, type.RankSpecifier); ArrayCreateExpression ace = (ArrayCreateExpression)lvd.Variables[0].Initializer; Assert.AreEqual(new int[] { 0 }, ace.CreateType.RankSpecifier); Assert.AreEqual(1, ace.Arguments.Count); Assert.AreEqual(10, ((PrimitiveExpression)ace.Arguments[0]).Value); }
public void CSharpNestedGenericLocalVariableDeclarationTest() { LocalVariableDeclaration lvd = ParseUtilCSharp.ParseStatement <LocalVariableDeclaration>("MyType<string>.InnerClass<int>.InnerInnerClass a;"); Assert.AreEqual(1, lvd.Variables.Count); InnerClassTypeReference ic = (InnerClassTypeReference)lvd.GetTypeForVariable(0); Assert.AreEqual("InnerInnerClass", ic.Type); Assert.AreEqual(0, ic.GenericTypes.Count); ic = (InnerClassTypeReference)ic.BaseType; Assert.AreEqual("InnerClass", ic.Type); Assert.AreEqual(1, ic.GenericTypes.Count); Assert.AreEqual("System.Int32", ic.GenericTypes[0].Type); Assert.AreEqual("MyType", ic.BaseType.Type); Assert.AreEqual(1, ic.BaseType.GenericTypes.Count); Assert.AreEqual("System.String", ic.BaseType.GenericTypes[0].Type); }
public void VBNetNestedGenericLocalVariableDeclarationTest() { LocalVariableDeclaration lvd = ParseUtilVBNet.ParseStatement <LocalVariableDeclaration>("Dim a as MyType(of string).InnerClass(of integer).InnerInnerClass"); Assert.AreEqual(1, lvd.Variables.Count); InnerClassTypeReference ic = (InnerClassTypeReference)lvd.GetTypeForVariable(0); Assert.AreEqual("InnerInnerClass", ic.Type); Assert.AreEqual(0, ic.GenericTypes.Count); ic = (InnerClassTypeReference)ic.BaseType; Assert.AreEqual("InnerClass", ic.Type); Assert.AreEqual(1, ic.GenericTypes.Count); Assert.AreEqual("System.Int32", ic.GenericTypes[0].Type); Assert.AreEqual("MyType", ic.BaseType.Type); Assert.AreEqual(1, ic.BaseType.GenericTypes.Count); Assert.AreEqual("System.String", ic.BaseType.GenericTypes[0].Type); }
public void CSharpComplexGenericLocalVariableDeclarationTest() { LocalVariableDeclaration lvd = ParseUtilCSharp.ParseStatement <LocalVariableDeclaration>("Generic<Namespace.Printable, G<Printable[]> > where = new Generic<Namespace.Printable, G<Printable[]>>();"); Assert.AreEqual(1, lvd.Variables.Count); Assert.AreEqual("where", ((VariableDeclaration)lvd.Variables[0]).Name); TypeReference type = lvd.GetTypeForVariable(0); Assert.AreEqual("Generic", type.Type); Assert.AreEqual(2, type.GenericTypes.Count); Assert.AreEqual("Namespace.Printable", type.GenericTypes[0].Type); Assert.AreEqual(0, type.GenericTypes[0].GenericTypes.Count); Assert.AreEqual("G", type.GenericTypes[1].Type); Assert.AreEqual(1, type.GenericTypes[1].GenericTypes.Count); Assert.AreEqual("Printable", type.GenericTypes[1].GenericTypes[0].Type); // TODO: Check initializer }
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data) { for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) { VariableDeclaration varDecl = (VariableDeclaration)localVariableDeclaration.Variables[i]; AddVariable(localVariableDeclaration.GetTypeForVariable(i), varDecl.Name, localVariableDeclaration.StartLocation, CurrentEndLocation, (localVariableDeclaration.Modifier & Modifiers.Const) == Modifiers.Const, false, varDecl.Initializer, null); } return base.VisitLocalVariableDeclaration(localVariableDeclaration, data); }
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data) { if (AddDefaultValueInitializerToLocalVariableDeclarations) { for (int i = 0; i < localVariableDeclaration.Variables.Count; i++) { VariableDeclaration decl = localVariableDeclaration.Variables[i]; if (decl.FixedArrayInitialization.IsNull && decl.Initializer.IsNull) { TypeReference type = localVariableDeclaration.GetTypeForVariable(i); decl.Initializer = ExpressionBuilder.CreateDefaultValueForType(type); } } } return base.VisitLocalVariableDeclaration(localVariableDeclaration, data); }
public object VisitLocalVariableDeclaration(LocalVariableDeclaration lvd, object data) { ArrayList list = new ArrayList(); for (int i = 0; i < lvd.Variables.Count; i++) { B.DeclarationStatement varDecl = new B.DeclarationStatement(GetLexicalInfo(lvd)); varDecl.Declaration = new B.Declaration(GetLexicalInfo(lvd.Variables[i]), lvd.Variables[i].Name, ConvertTypeReference(lvd.GetTypeForVariable(i))); varDecl.Initializer = ConvertExpression(lvd.Variables[i].Initializer); list.Add(varDecl); } return(list); }
void ConvertForStatement(ForStatement forStatement) { // ForStatement -> ForNextStatement when for-loop is simple // only the following forms of the for-statement are allowed: // for (TypeReference name = start; name < oneAfterEnd; name += step) // for (name = start; name < oneAfterEnd; name += step) // for (TypeReference name = start; name <= end; name += step) // for (name = start; name <= end; name += step) // for (TypeReference name = start; name > oneAfterEnd; name -= step) // for (name = start; name > oneAfterEnd; name -= step) // for (TypeReference name = start; name >= end; name -= step) // for (name = start; name >= end; name -= step) // check if the form is valid and collect TypeReference, name, start, end and step if (forStatement.Initializers.Count != 1) { return; } if (forStatement.Iterator.Count != 1) { return; } StatementExpression statement = forStatement.Iterator[0] as StatementExpression; if (statement == null) { return; } AssignmentExpression iterator = statement.Expression as AssignmentExpression; if (iterator == null || (iterator.Op != AssignmentOperatorType.Add && iterator.Op != AssignmentOperatorType.Subtract)) { return; } IdentifierExpression iteratorIdentifier = iterator.Left as IdentifierExpression; if (iteratorIdentifier == null) { return; } PrimitiveExpression stepExpression = iterator.Right as PrimitiveExpression; if (stepExpression == null || !(stepExpression.Value is int)) { return; } int step = (int)stepExpression.Value; if (iterator.Op == AssignmentOperatorType.Subtract) { step = -step; } BinaryOperatorExpression condition = forStatement.Condition as BinaryOperatorExpression; if (condition == null || !(condition.Left is IdentifierExpression)) { return; } if ((condition.Left as IdentifierExpression).Identifier != iteratorIdentifier.Identifier) { return; } Expression end; if (iterator.Op == AssignmentOperatorType.Subtract) { if (condition.Op == BinaryOperatorType.GreaterThanOrEqual) { end = condition.Right; } else if (condition.Op == BinaryOperatorType.GreaterThan) { end = Expression.AddInteger(condition.Right, 1); } else { return; } } else { if (condition.Op == BinaryOperatorType.LessThanOrEqual) { end = condition.Right; } else if (condition.Op == BinaryOperatorType.LessThan) { end = Expression.AddInteger(condition.Right, -1); } else { return; } } Expression start; TypeReference typeReference = null; LocalVariableDeclaration varDecl = forStatement.Initializers[0] as LocalVariableDeclaration; if (varDecl != null) { if (varDecl.Variables.Count != 1 || varDecl.Variables[0].Name != iteratorIdentifier.Identifier || varDecl.Variables[0].Initializer == null) { return; } typeReference = varDecl.GetTypeForVariable(0); start = varDecl.Variables[0].Initializer; } else { statement = forStatement.Initializers[0] as StatementExpression; if (statement == null) { return; } AssignmentExpression assign = statement.Expression as AssignmentExpression; if (assign == null || assign.Op != AssignmentOperatorType.Assign) { return; } if (!(assign.Left is IdentifierExpression)) { return; } if ((assign.Left as IdentifierExpression).Identifier != iteratorIdentifier.Identifier) { return; } start = assign.Right; } ForNextStatement forNextStatement = new ForNextStatement(typeReference, iteratorIdentifier.Identifier, start, end, (step == 1) ? null : new PrimitiveExpression(step, step.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)), forStatement.EmbeddedStatement, null); Statement.Replace(forStatement, forNextStatement); }
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data) { CodeVariableDeclarationStatement declStmt = null; for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) { CodeTypeReference type = ConvType(localVariableDeclaration.GetTypeForVariable(i) ?? new TypeReference("System.Object", true)); VariableDeclaration var = (VariableDeclaration)localVariableDeclaration.Variables[i]; if (!var.Initializer.IsNull) { declStmt = new CodeVariableDeclarationStatement(type, var.Name, (CodeExpression)((INode)var.Initializer).AcceptVisitor(this, data)); } else { declStmt = new CodeVariableDeclarationStatement(type, var.Name); } variables.Add(declStmt); AddStmt(declStmt); } return declStmt; }