public override object TrackedVisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { string targetString; if (projectTypes) { targetString = GetTargetString(fieldReferenceExpression); if (CodeBase.Mappings.Contains(targetString)) { TypeReferenceExpression typeExpression = new TypeReferenceExpression(targetString); typeExpression.Parent = fieldReferenceExpression.Parent; ReplaceCurrentNode(typeExpression); return null; } } else if (!IsMethodInvocation(fieldReferenceExpression) && ReachToInvocation(fieldReferenceExpression)) { targetString = GetTargetString(fieldReferenceExpression); if (targetString.StartsWith("id")) return null; string suffix = "__"; if (targetString.IndexOf(suffix) != -1) { if (targetString.EndsWith(suffix)) targetString = targetString.Substring(0, targetString.Length - suffix.Length); else return base.TrackedVisitFieldReferenceExpression(fieldReferenceExpression, data); } TypeReferenceExpression typeExpression = new TypeReferenceExpression(targetString); typeExpression.Parent = fieldReferenceExpression.Parent; ReplaceCurrentNode(typeExpression); return null; } return base.TrackedVisitFieldReferenceExpression(fieldReferenceExpression, data); }
public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data) { TypeDeclaration typeDeclaration = (TypeDeclaration) AstUtil.GetParentOfType(identifierExpression, typeof(TypeDeclaration)); if (typeDeclaration != null && typeDeclaration.Parent is TypeDeclaration && !IsInvocation(identifierExpression)) { IList parentFields = AstUtil.GetChildrenWithType(typeDeclaration.Parent, typeof(FieldDeclaration)); IList innerFields = AstUtil.GetChildrenWithType(typeDeclaration, typeof(FieldDeclaration)); FieldDeclaration field = new FieldDeclaration(null); field.Fields.Add(new VariableDeclaration(identifierExpression.Identifier)); if (!ContainsField(innerFields, field, false) && !IdentifierDeclaredInParameter(identifierExpression) && ContainsField(parentFields, field, false)) { string parentTypeName = ((TypeDeclaration) typeDeclaration.Parent).Name; AddInstanceField(typeDeclaration, parentTypeName); IdentifierExpression ins = new IdentifierExpression(parentTypeName); FieldReferenceExpression fieldReferenceExpression = new FieldReferenceExpression(ins, identifierExpression.Identifier); fieldReferenceExpression.Parent = identifierExpression.Parent; ReplaceCurrentNode(fieldReferenceExpression); } } return base.TrackedVisitIdentifierExpression(identifierExpression, data); }
public override object TrackedVisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { Expression invoker = fieldReferenceExpression.TargetObject; TypeReference invokerType = GetExpressionType(invoker); if (invokerType != null) { string returnType = GetFullName(invokerType); if (invokerType.RankSpecifier != null && invokerType.RankSpecifier.Length > 0) { returnType = "JavaArray"; } TypeMapping mapping = CodeBase.Mappings[returnType]; Expression[] replacedExpression; string key; if (ContainsMapping(mapping, fieldReferenceExpression, out key)) { replacedExpression = GetReplacedExpression(fieldReferenceExpression, key, mapping.Members); ReplaceCurrentNode(fieldReferenceExpression, replacedExpression, data); } else { mapping = GetProperMapping(invokerType, fieldReferenceExpression, fieldReferenceExpression.FieldName, out key); if (key != null) { replacedExpression = GetReplacedExpression(fieldReferenceExpression, key, mapping.Members); ReplaceCurrentNode(fieldReferenceExpression, replacedExpression, data); } } } return base.TrackedVisitFieldReferenceExpression(fieldReferenceExpression, data); }
public override void GenerateCode(List<AbstractNode> nodes, IList items) { TypeReference stringReference = new TypeReference("System.String"); MethodDeclaration method = new MethodDeclaration("ToString", Modifiers.Public | Modifiers.Override, stringReference, null, null); method.Body = new BlockStatement(); Expression target = new FieldReferenceExpression(new TypeReferenceExpression(stringReference), "Format"); InvocationExpression methodCall = new InvocationExpression(target); StringBuilder formatString = new StringBuilder(); formatString.Append('['); formatString.Append(currentClass.Name); for (int i = 0; i < items.Count; i++) { formatString.Append(' '); formatString.Append(codeGen.GetPropertyName(((FieldWrapper)items[i]).Field.Name)); formatString.Append("={"); formatString.Append(i); formatString.Append('}'); } formatString.Append(']'); methodCall.Arguments.Add(new PrimitiveExpression(formatString.ToString(), formatString.ToString())); foreach (FieldWrapper w in items) { methodCall.Arguments.Add(new FieldReferenceExpression(new ThisReferenceExpression(), w.Field.Name)); } method.Body.AddChild(new ReturnStatement(methodCall)); nodes.Add(method); }
public override object TrackedVisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { string field = fieldReferenceExpression.FieldName; if (keywords.Contains(field)) { fieldReferenceExpression.FieldName = "_" + field; } return base.TrackedVisitFieldReferenceExpression(fieldReferenceExpression, data); }
public void MethodOnThisReferenceInvocation() { // InitializeComponents(); FieldReferenceExpression field = new FieldReferenceExpression(new ThisReferenceExpression(), "InitializeComponents"); InvocationExpression invocation = new InvocationExpression(field, new List<Expression>()); object output = invocation.AcceptVisitor(new CodeDomVisitor(), null); Assert.IsTrue(output is CodeMethodInvokeExpression); CodeMethodInvokeExpression mie = (CodeMethodInvokeExpression)output; Assert.AreEqual("InitializeComponents", mie.Method.MethodName); Assert.IsTrue(mie.Method.TargetObject is CodeThisReferenceExpression); }
private InvocationExpression CreateGetClassMethodInvocation(TypeOfExpression typeOfExpression) { FieldReferenceExpression argument = new FieldReferenceExpression(typeOfExpression, "AssemblyQualifiedName"); typeOfExpression.Parent = argument; List<Expression> arguments = new List<Expression>(); arguments.Add(argument); IdentifierExpression methodIdentifier = new IdentifierExpression("java.lang.Class"); FieldReferenceExpression methodReference = new FieldReferenceExpression(methodIdentifier, "forName"); InvocationExpression invocationExpression = new InvocationExpression(methodReference, arguments); argument.Parent = invocationExpression; methodReference.Parent = invocationExpression; return invocationExpression; }
private bool IsMethodInvocation(FieldReferenceExpression fieldReference) { if (fieldReference.Parent is InvocationExpression) { InvocationExpression invocationExpression = (InvocationExpression) fieldReference.Parent; if (invocationExpression.TargetObject is FieldReferenceExpression) { FieldReferenceExpression invocationReference = (FieldReferenceExpression) invocationExpression.TargetObject; return (fieldReference.FieldName == invocationReference.FieldName); } } return false; }
public void InvocationOfStaticMethod() { // System.Drawing.Color.FromArgb(); FieldReferenceExpression field = new FieldReferenceExpression(new IdentifierExpression("System"), "Drawing"); field = new FieldReferenceExpression(field, "Color"); field = new FieldReferenceExpression(field, "FromArgb"); InvocationExpression invocation = new InvocationExpression(field, new List<Expression>()); object output = invocation.AcceptVisitor(new CodeDomVisitor(), null); Assert.IsTrue(output is CodeMethodInvokeExpression); CodeMethodInvokeExpression mie = (CodeMethodInvokeExpression)output; Assert.AreEqual("FromArgb", mie.Method.MethodName); Assert.IsTrue(mie.Method.TargetObject is CodeTypeReferenceExpression); Assert.AreEqual("System.Drawing.Color", (mie.Method.TargetObject as CodeTypeReferenceExpression).Type.BaseType); }
public override object TrackedVisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { string fieldName = fieldReferenceExpression.FieldName; if (fields.Contains(fieldName)) { if (fieldReferenceExpression.TargetObject is IdentifierExpression) { IdentifierExpression identifierExpression = (IdentifierExpression) fieldReferenceExpression.TargetObject; if (identifierExpression.Identifier == "System") fieldReferenceExpression.FieldName = (string) fields[fieldName]; } } return base.TrackedVisitFieldReferenceExpression(fieldReferenceExpression, data); }
public override void GenerateCode(List<AbstractNode> nodes, IList items) { ConstructorDeclaration ctor = new ConstructorDeclaration(currentClass.Name, Modifiers.Public, null, null); ctor.Body = new BlockStatement(); foreach (FieldWrapper w in items) { string parameterName = codeGen.GetParameterName(w.Field.Name); ctor.Parameters.Add(new ParameterDeclarationExpression(ConvertType(w.Field.ReturnType), parameterName)); Expression left = new FieldReferenceExpression(new ThisReferenceExpression(), w.Field.Name); Expression right = new IdentifierExpression(parameterName); Expression expr = new AssignmentExpression(left, AssignmentOperatorType.Assign, right); ctor.Body.AddChild(new ExpressionStatement(expr)); } nodes.Add(ctor); }
public override object TrackedVisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { if (!IsMethodInvocation(fieldReferenceExpression)) { TypeReference typeReference = GetExpressionType(fieldReferenceExpression.TargetObject); if (typeReference != null) { string fullName = GetFullName(typeReference); string key = fullName + "." + fieldReferenceExpression.FieldName; if (CodeBase.References.Contains(key)) fieldReferenceExpression.FieldName = (string) CodeBase.References[key]; } } return base.TrackedVisitFieldReferenceExpression(fieldReferenceExpression, data); }
public override object TrackedVisitMethodDeclaration(MethodDeclaration methodDeclaration, object data) { if (AstUtil.ContainsModifier(methodDeclaration, Modifiers.Synchronized)) { List<Expression> positionalArgs = new List<Expression>(); TypeReferenceExpression system = new TypeReferenceExpression("System.Runtime.CompilerServices.MethodImplOptions"); FieldReferenceExpression attributeArgument = new FieldReferenceExpression(system, "Synchronized"); positionalArgs.Add(attributeArgument); AttributeSection attributeSection = CreateAttributeSection("System.Runtime.CompilerServices.MethodImplAttribute", positionalArgs); MethodDeclaration replacedMethod = methodDeclaration; replacedMethod.Attributes.Add(attributeSection); attributeSection.Parent = replacedMethod; ReplaceCurrentNode(replacedMethod); } return base.TrackedVisitMethodDeclaration(methodDeclaration, data); }
private void CreateMethodImplementation(MethodDeclaration equalsMethod) { string fieldName = "instancehelper_" + equalsMethod.Name; IdentifierExpression targetObject = new IdentifierExpression("java.lang.Object"); List<Expression> arguments = new List<Expression>(); arguments.Add(new ThisReferenceExpression()); string parameterName = ((ParameterDeclarationExpression) equalsMethod.Parameters[0]).ParameterName; IdentifierExpression identifier = new IdentifierExpression(parameterName); arguments.Add(identifier); FieldReferenceExpression fieldReferenceExpression = new FieldReferenceExpression(targetObject, fieldName); InvocationExpression invocationExpression = new InvocationExpression(fieldReferenceExpression, arguments); ReturnStatement returnStatement = new ReturnStatement(invocationExpression); BlockStatement block = new BlockStatement(); block.Children.Add(returnStatement); equalsMethod.Body = block; }
public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data) { if (data != null) { string identifier = identifierExpression.Identifier; DataObject dataObject = (DataObject) data; if (identifier != "enclosingInstance" && !IsInvocation(identifierExpression) && !dataObject.list.Contains(identifier)) { string staticFullName = GetStaticFullName(identifier, identifierExpression.Parent); if (staticFullName == null || !CodeBase.Types.Contains(staticFullName)) { TypeReference typeReference = GetExpressionType(identifierExpression, dataObject.objectCreation); if (typeReference != null) { if (!unknownFields.Contains(identifier)) unknownFields.Add(identifier, typeReference); if (!dataObject.list.Contains(identifier)) dataObject.list.Add(identifier, typeReference); } } } else if (IsInvocation(identifierExpression)) { TypeDeclaration typeDeclaration = dataObject.anonymous; if (typeDeclaration != null && typeDeclaration.Name.StartsWith("AnonymousClass")) { TypeDeclaration enclosingType = (TypeDeclaration) typeDeclaration.Parent; IList enclosingMethods = GetAccessibleMethods(enclosingType); if (ContainsMethod(enclosingMethods, identifier)) { FieldReferenceExpression fieldReferenceExpression = new FieldReferenceExpression(new IdentifierExpression("enclosingInstance"), identifier); fieldReferenceExpression.Parent = identifierExpression.Parent; ReplaceCurrentNode(fieldReferenceExpression); } } } } return null; }
public sealed override object VisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { this.BeginVisit(fieldReferenceExpression); object result = this.TrackedVisit(fieldReferenceExpression, data); this.EndVisit(fieldReferenceExpression); return result; }
void ReDimClauseInternal( #line 2622 "VBNET.ATG" ref Expression expr) { #line 2623 "VBNET.ATG" List<Expression> arguments; bool canBeNormal; bool canBeRedim; string name; while (la.kind == 10 || #line 2626 "VBNET.ATG" la.kind == Tokens.OpenParenthesis && Peek(1).kind == Tokens.Of) { if (la.kind == 10) { lexer.NextToken(); IdentifierOrKeyword( #line 2625 "VBNET.ATG" out name); #line 2625 "VBNET.ATG" expr = new FieldReferenceExpression(expr, name); } else { InvocationExpression( #line 2627 "VBNET.ATG" ref expr); } } Expect(24); NormalOrReDimArgumentList( #line 2630 "VBNET.ATG" out arguments, out canBeNormal, out canBeRedim); Expect(25); #line 2632 "VBNET.ATG" expr = new InvocationExpression(expr, arguments); if (canBeRedim == false || canBeNormal && (la.kind == Tokens.Dot || la.kind == Tokens.OpenParenthesis)) { if (this.Errors.Count == 0) { // don't recurse on parse errors - could result in endless recursion ReDimClauseInternal(ref expr); } } }
void InvocationExpression( #line 1624 "VBNET.ATG" ref Expression pexpr) { #line 1625 "VBNET.ATG" List<TypeReference> typeParameters = new List<TypeReference>(); List<Expression> parameters = null; TypeReference type; Expect(24); #line 1629 "VBNET.ATG" Location start = t.Location; if (la.kind == 200) { lexer.NextToken(); TypeName( #line 1631 "VBNET.ATG" out type); #line 1631 "VBNET.ATG" if (type != null) typeParameters.Add(type); while (la.kind == 12) { lexer.NextToken(); TypeName( #line 1634 "VBNET.ATG" out type); #line 1634 "VBNET.ATG" if (type != null) typeParameters.Add(type); } Expect(25); if (la.kind == 10) { lexer.NextToken(); Identifier(); #line 1639 "VBNET.ATG" pexpr = new FieldReferenceExpression(GetTypeReferenceExpression(pexpr, typeParameters), t.val); } else if (la.kind == 24) { lexer.NextToken(); ArgumentList( #line 1641 "VBNET.ATG" out parameters); Expect(25); #line 1643 "VBNET.ATG" pexpr = new InvocationExpression(pexpr, parameters, typeParameters); } else SynErr(244); } else if (StartOf(30)) { ArgumentList( #line 1645 "VBNET.ATG" out parameters); Expect(25); #line 1647 "VBNET.ATG" pexpr = new InvocationExpression(pexpr, parameters, typeParameters); } else SynErr(245); #line 1649 "VBNET.ATG" pexpr.StartLocation = start; pexpr.EndLocation = t.Location; }
void SimpleNonInvocationExpression( #line 1567 "VBNET.ATG" out Expression pexpr) { #line 1569 "VBNET.ATG" Expression expr; TypeReference type = null; string name = String.Empty; pexpr = null; if (StartOf(29)) { switch (la.kind) { case 3: { lexer.NextToken(); #line 1577 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val); break; } case 4: { lexer.NextToken(); #line 1578 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val); break; } case 7: { lexer.NextToken(); #line 1579 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val); break; } case 6: { lexer.NextToken(); #line 1580 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val); break; } case 5: { lexer.NextToken(); #line 1581 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val); break; } case 9: { lexer.NextToken(); #line 1582 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val); break; } case 8: { lexer.NextToken(); #line 1583 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val); break; } case 173: { lexer.NextToken(); #line 1585 "VBNET.ATG" pexpr = new PrimitiveExpression(true, "true"); break; } case 96: { lexer.NextToken(); #line 1586 "VBNET.ATG" pexpr = new PrimitiveExpression(false, "false"); break; } case 130: { lexer.NextToken(); #line 1587 "VBNET.ATG" pexpr = new PrimitiveExpression(null, "null"); break; } case 24: { lexer.NextToken(); Expr( #line 1588 "VBNET.ATG" out expr); Expect(25); #line 1588 "VBNET.ATG" pexpr = new ParenthesizedExpression(expr); break; } case 2: case 47: case 49: case 50: case 51: case 70: case 144: case 169: case 176: case 177: case 204: { Identifier(); #line 1589 "VBNET.ATG" pexpr = new IdentifierExpression(t.val); break; } case 52: case 54: case 65: case 76: case 77: case 84: case 111: case 117: case 133: case 159: case 160: case 165: case 190: case 191: case 192: case 193: { #line 1590 "VBNET.ATG" string val = String.Empty; if (StartOf(10)) { PrimitiveTypeName( #line 1591 "VBNET.ATG" out val); } else if (la.kind == 133) { lexer.NextToken(); #line 1591 "VBNET.ATG" val = "Object"; } else SynErr(240); Expect(10); #line 1592 "VBNET.ATG" t.val = ""; Identifier(); #line 1592 "VBNET.ATG" pexpr = new FieldReferenceExpression(new TypeReferenceExpression(val), t.val); break; } case 119: { lexer.NextToken(); #line 1593 "VBNET.ATG" pexpr = new ThisReferenceExpression(); break; } case 124: case 125: { #line 1594 "VBNET.ATG" Expression retExpr = null; if (la.kind == 124) { lexer.NextToken(); #line 1595 "VBNET.ATG" retExpr = new BaseReferenceExpression(); } else if (la.kind == 125) { lexer.NextToken(); #line 1596 "VBNET.ATG" retExpr = new ClassReferenceExpression(); } else SynErr(241); Expect(10); IdentifierOrKeyword( #line 1598 "VBNET.ATG" out name); #line 1598 "VBNET.ATG" pexpr = new FieldReferenceExpression(retExpr, name); break; } case 198: { lexer.NextToken(); Expect(10); Identifier(); #line 1600 "VBNET.ATG" type = new TypeReference(t.val ?? ""); #line 1602 "VBNET.ATG" type.IsGlobal = true; #line 1603 "VBNET.ATG" pexpr = new TypeReferenceExpression(type); break; } case 127: { ObjectCreateExpression( #line 1604 "VBNET.ATG" out expr); #line 1604 "VBNET.ATG" pexpr = expr; break; } case 75: case 82: case 199: { #line 1606 "VBNET.ATG" CastType castType = CastType.Cast; if (la.kind == 82) { lexer.NextToken(); } else if (la.kind == 75) { lexer.NextToken(); #line 1608 "VBNET.ATG" castType = CastType.Conversion; } else if (la.kind == 199) { lexer.NextToken(); #line 1609 "VBNET.ATG" castType = CastType.TryCast; } else SynErr(242); Expect(24); Expr( #line 1611 "VBNET.ATG" out expr); Expect(12); TypeName( #line 1611 "VBNET.ATG" out type); Expect(25); #line 1612 "VBNET.ATG" pexpr = new CastExpression(type, expr, castType); break; } case 59: case 60: case 61: case 62: case 63: case 64: case 66: case 68: case 69: case 72: case 73: case 74: case 194: case 195: case 196: case 197: { CastTarget( #line 1613 "VBNET.ATG" out type); Expect(24); Expr( #line 1613 "VBNET.ATG" out expr); Expect(25); #line 1613 "VBNET.ATG" pexpr = new CastExpression(type, expr, CastType.PrimitiveConversion); break; } case 43: { lexer.NextToken(); Expr( #line 1614 "VBNET.ATG" out expr); #line 1614 "VBNET.ATG" pexpr = new AddressOfExpression(expr); break; } case 102: { lexer.NextToken(); Expect(24); GetTypeTypeName( #line 1615 "VBNET.ATG" out type); Expect(25); #line 1615 "VBNET.ATG" pexpr = new TypeOfExpression(type); break; } case 175: { lexer.NextToken(); SimpleExpr( #line 1616 "VBNET.ATG" out expr); Expect(113); TypeName( #line 1616 "VBNET.ATG" out type); #line 1616 "VBNET.ATG" pexpr = new TypeOfIsExpression(expr, type); break; } } } else if (la.kind == 10) { lexer.NextToken(); IdentifierOrKeyword( #line 1620 "VBNET.ATG" out name); #line 1620 "VBNET.ATG" pexpr = new FieldReferenceExpression(null, name); } else SynErr(243); }
void SimpleExpr( #line 1558 "VBNET.ATG" out Expression pexpr) { SimpleNonInvocationExpression( #line 1560 "VBNET.ATG" out pexpr); while (la.kind == 10 || la.kind == 24) { if (la.kind == 10) { #line 1561 "VBNET.ATG" string name; lexer.NextToken(); IdentifierOrKeyword( #line 1562 "VBNET.ATG" out name); #line 1562 "VBNET.ATG" pexpr = new FieldReferenceExpression(pexpr, name); } else { InvocationExpression( #line 1563 "VBNET.ATG" ref pexpr); } } }
public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data) { if (invocationExpression.TargetObject is IdentifierExpression) { TypeDeclaration typeDeclaration = (TypeDeclaration) AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration)); IdentifierExpression methodIdentifier = (IdentifierExpression) invocationExpression.TargetObject; if (typeDeclaration.Parent is TypeDeclaration) { List<ParameterDeclarationExpression> argList = new List<ParameterDeclarationExpression>(); int i = 0; foreach (Expression argument in invocationExpression.Arguments) { TypeReference argumentType = GetExpressionType(argument); if (argumentType != null) { string argType = argumentType.Type; TypeReference typeReference = new TypeReference(argType); ParameterDeclarationExpression parameterExpression = new ParameterDeclarationExpression(typeReference, "arg" + i); parameterExpression.TypeReference.RankSpecifier = new int[0]; i++; argList.Add(parameterExpression); } } MethodDeclaration argMethod = new MethodDeclaration(methodIdentifier.Identifier, Modifiers.None, null, argList, null); IList parentMethods = GetAccessibleMethods((TypeDeclaration) typeDeclaration.Parent); if (Contains(parentMethods, argMethod)) { int methodIndex = IndexOf(parentMethods, argMethod); argMethod = (MethodDeclaration) parentMethods[methodIndex]; if (!AstUtil.ContainsModifier(argMethod, Modifiers.Static)) { string parentTypeName = ((TypeDeclaration) typeDeclaration.Parent).Name; AddInstanceField(typeDeclaration, parentTypeName); AddProperConstructor(typeDeclaration, parentTypeName); FieldReferenceExpression newReference = new FieldReferenceExpression( new IdentifierExpression(parentTypeName), ((IdentifierExpression) invocationExpression.TargetObject).Identifier); InvocationExpression newInvication = new InvocationExpression(newReference, invocationExpression.Arguments); newInvication.Parent = invocationExpression.Parent; ReplaceCurrentNode(newInvication); } } } } return base.TrackedVisitInvocationExpression(invocationExpression, data); }
public override object VisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { fieldReferenceExpression.TargetObject.Parent = fieldReferenceExpression; return base.VisitFieldReferenceExpression(fieldReferenceExpression, data); }
protected Expression GetReplacedExpression(Expression expression, string mapKey) { bool removeId = false; bool isCurrent = false; if (mapKey.StartsWith("!")) { removeId = true; mapKey = mapKey.Substring(1); } else if (mapKey.StartsWith("=")) { mapKey = mapKey.Substring(1); isCurrent = true; } AssignmentExpression mappedExpression = (AssignmentExpression) GetMapExpression(mapKey); Substitution substitution = new Substitution(); List<Expression> parameters = GetParameters(expression); Expression targetId = GetTargetObject(expression); substitution.Identifier = targetId; if (!removeId) { if (expression is InvocationExpression) { InvocationExpression invocationExpression = (InvocationExpression) expression; if (invocationExpression.TargetObject is FieldReferenceExpression) { FieldReferenceExpression invocationTarget = (FieldReferenceExpression) invocationExpression.TargetObject; if (mappedExpression.Right is InvocationExpression) { IdentifierExpression identifierExpression = GetIdentifierExpression((InvocationExpression) mappedExpression.Right); if (identifierExpression != null) { FieldReferenceExpression referenceExpression = new FieldReferenceExpression(invocationTarget.TargetObject, identifierExpression.Identifier); Expression identifierParent = (Expression) identifierExpression.Parent; if (identifierParent is InvocationExpression) ((InvocationExpression) identifierParent).TargetObject = referenceExpression; } } else if (mappedExpression.Right is IdentifierExpression) { FieldReferenceExpression referenceExpression = new FieldReferenceExpression(invocationTarget.TargetObject, ((IdentifierExpression) mappedExpression.Right).Identifier); mappedExpression.Right = referenceExpression; } } } } substitution.Substitute(mappedExpression, parameters); mappedExpression.Right.Parent = expression.Parent; if (isCurrent) mappedExpression.Right.StartLocation = new Location(-1, -1); return mappedExpression.Right; }
public override object VisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { if (fieldReferenceExpression.TargetObject is ThisReferenceExpression) { string name = fieldReferenceExpression.FieldName; foreach (VariableDeclaration var in fields) { if (var.Name == name) { fieldReferenceExpression.FieldName = prefix + name; break; } } } return base.VisitFieldReferenceExpression(fieldReferenceExpression, data); }
private void CheckThroughParents(TypeDeclaration typeDeclaration, IdentifierExpression identifierExpression) { if (typeDeclaration.Parent is TypeDeclaration) { TypeDeclaration parent = (TypeDeclaration) typeDeclaration.Parent; CheckThroughParents(parent, identifierExpression); } if (typeDeclaration.BaseTypes.Count > 0) { foreach (TypeReference baseType in typeDeclaration.BaseTypes) { string fullName = GetFullName(baseType); if (CodeBase.References.Contains(fullName)) { string referedBaseType = (string) CodeBase.References[fullName]; TypeReference newBaseType = AstUtil.GetTypeReference(referedBaseType, baseType.Parent); string referenceBaseType = GetFullName(newBaseType); TypeDeclaration baseTypeDeclaration = (TypeDeclaration) CodeBase.Types[referenceBaseType]; if (baseTypeDeclaration != null) { if (DefinedInFieldsClass(baseTypeDeclaration, identifierExpression.Identifier)) { TypeReferenceExpression id = new TypeReferenceExpression(referedBaseType); FieldReferenceExpression replaced = new FieldReferenceExpression(id, identifierExpression.Identifier); replaced.Parent = identifierExpression.Parent; ReplaceCurrentNode(replaced); } else { TypeDeclaration type = (TypeDeclaration) CodeBase.Types[fullName]; CheckThroughParents(type, identifierExpression); } } } else if (CodeBase.Types.Contains(fullName)) { TypeDeclaration baseTypeDeclaration = (TypeDeclaration) CodeBase.Types[fullName]; CheckThroughParents(baseTypeDeclaration, identifierExpression); } } } }
public virtual object TrackedVisit(FieldReferenceExpression fieldReferenceExpression, object data) { return base.VisitFieldReferenceExpression(fieldReferenceExpression, data); }
public virtual object VisitFieldReferenceExpression(FieldReferenceExpression fieldReferenceExpression, object data) { Debug.Assert((fieldReferenceExpression != null)); Debug.Assert((fieldReferenceExpression.TargetObject != null)); return fieldReferenceExpression.TargetObject.AcceptVisitor(this, data); }
private void AddProperConstructor(TypeDeclaration typeDeclaration, string instanceFieldName) { TypeReference type = new TypeReference(instanceFieldName); ParameterDeclarationExpression fieldParameter = new ParameterDeclarationExpression(type, instanceFieldName); FieldReferenceExpression fieldReference = new FieldReferenceExpression(new ThisReferenceExpression(), instanceFieldName); IdentifierExpression right = new IdentifierExpression(instanceFieldName); AssignmentExpression assignment = new AssignmentExpression(fieldReference, AssignmentOperatorType.Assign, right); ExpressionStatement expressionStatement = new ExpressionStatement(assignment); string fullName = GetFullName(typeDeclaration); IList constructors = AstUtil.GetChildrenWithType(typeDeclaration, typeof(ConstructorDeclaration)); if (constructors.Count == 0) { string name = typeDeclaration.Name; List<ParameterDeclarationExpression> parameters = new List<ParameterDeclarationExpression>(); parameters.Add(fieldParameter); ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(name, Modifiers.Public, parameters, null); constructorDeclaration.Body = new BlockStatement(); constructorDeclaration.Body.AddChild(expressionStatement); constructorDeclaration.Parent = typeDeclaration; CodeBase.References.Add("Cons:" + fullName, null); typeDeclaration.Children.Add(constructorDeclaration); } else { foreach (ConstructorDeclaration constructor in constructors) { if (!ContainsParameter(constructor.Parameters, fieldParameter)) { constructor.Parameters.Add(fieldParameter); if (constructor.ConstructorInitializer != null) { ConstructorInitializer ci = constructor.ConstructorInitializer; if (ci.ConstructorInitializerType == ConstructorInitializerType.This) ci.Arguments.Add(new IdentifierExpression(fieldParameter.ParameterName)); } constructor.Body.Children.Insert(0, expressionStatement); } } if (!CodeBase.References.Contains("Cons:" + fullName)) CodeBase.References.Add("Cons:" + fullName, null); } }
public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data) { Expression targetObject = invocationExpression.TargetObject; string methodName = null; if (targetObject is IdentifierExpression) methodName = ((IdentifierExpression) targetObject).Identifier; else if (targetObject is FieldReferenceExpression) methodName = ((FieldReferenceExpression) targetObject).FieldName; if (methodName.StartsWith("set") || methodName.StartsWith("get")) { if (targetObject is IdentifierExpression) { TypeDeclaration typeDeclaration = (TypeDeclaration) AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration)); string key; if (ContainsReferences(typeDeclaration, methodName, out key)) { IdentifierExpression identifierExpression = new IdentifierExpression((string) CodeBase.References[key]); if (methodName.StartsWith("get")) { identifierExpression.Parent = invocationExpression.Parent; ReplaceCurrentNode(identifierExpression); } else if (methodName.StartsWith("set")) { Expression setValue = (Expression) invocationExpression.Arguments[0]; AssignmentExpression assignment = new AssignmentExpression(identifierExpression, AssignmentOperatorType.Assign, setValue); assignment.Parent = invocationExpression.Parent; ReplaceCurrentNode(assignment); assignment.AcceptVisitor(this, data); } } } else if (targetObject is FieldReferenceExpression) { Expression target = ((FieldReferenceExpression) targetObject).TargetObject; TypeReference invokerType = GetExpressionType(target); if (invokerType != null) { string fullName = GetFullName(invokerType); string key = fullName + "." + methodName; if (CodeBase.References.Contains(key)) { string property = (string) CodeBase.References[key]; FieldReferenceExpression replaced = new FieldReferenceExpression(target, property); if (methodName.StartsWith("get")) { replaced.Parent = invocationExpression.Parent; ReplaceCurrentNode(replaced); replaced.AcceptVisitor(this, data); } else { Expression setValue = (Expression) invocationExpression.Arguments[0]; AssignmentExpression assignment = new AssignmentExpression(replaced, AssignmentOperatorType.Assign, setValue); assignment.Parent = invocationExpression.Parent; ReplaceCurrentNode(assignment); assignment.AcceptVisitor(this, data); } } } } } return base.TrackedVisitInvocationExpression(invocationExpression, data); }
private void AddAnonymousClassConstructor(ObjectCreateExpression obc, TypeDeclaration typeDeclaration, List<ParameterDeclarationExpression> parameters) { List<ParameterDeclarationExpression> constructorParameters = new List<ParameterDeclarationExpression>(); List<ParameterDeclarationExpression> baseArguments = GetBaseConstructorParameters(obc); constructorParameters.AddRange(baseArguments); constructorParameters.AddRange(parameters); ConstructorDeclaration constructor = new ConstructorDeclaration(typeDeclaration.Name, Modifiers.Public, constructorParameters, null); constructor.Body = new BlockStatement(); constructor.Parent = typeDeclaration; string initializerType = null; IList constructors = AstUtil.GetChildrenWithType(typeDeclaration, typeof(ConstructorDeclaration)); if (constructors.Count > 0) initializerType = "this"; else if (baseArguments.Count > 0) initializerType = "base"; InitializeConstructor(constructor, baseArguments, initializerType); foreach (ParameterDeclarationExpression paremeter in parameters) { ThisReferenceExpression thisReference = new ThisReferenceExpression(); FieldReferenceExpression left = new FieldReferenceExpression(thisReference, paremeter.ParameterName); IdentifierExpression right = new IdentifierExpression(paremeter.ParameterName); AssignmentExpression assignment = new AssignmentExpression(left, AssignmentOperatorType.Assign, right); Statement stm = new ExpressionStatement(assignment); constructor.Body.Children.Add(stm); } typeDeclaration.Children.Insert(0, constructor); }