public void MethodOnThisReferenceInvocation() { // InitializeComponents(); MemberReferenceExpression field = new MemberReferenceExpression(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); }
public void InvocationOfStaticMethod() { // System.Drawing.Color.FromArgb(); MemberReferenceExpression field = new MemberReferenceExpression(new IdentifierExpression("System"), "Drawing"); field = new MemberReferenceExpression(field, "Color"); field = new MemberReferenceExpression(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 VisitMemberReferenceExpression(MemberReferenceExpression fieldReferenceExpression, object data) { if (fieldReferenceExpression.TargetObject is ThisReferenceExpression) { string name = fieldReferenceExpression.MemberName; foreach (VariableDeclaration var in fields) { if (var.Name == name) { fieldReferenceExpression.MemberName = prefix + name; break; } } } return base.VisitMemberReferenceExpression(fieldReferenceExpression, data); }
public virtual object VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) { Debug.Assert((memberReferenceExpression != null)); Debug.Assert((memberReferenceExpression.TargetObject != null)); Debug.Assert((memberReferenceExpression.TypeArguments != null)); memberReferenceExpression.TargetObject.AcceptVisitor(this, data); foreach (TypeReference o in memberReferenceExpression.TypeArguments) { Debug.Assert(o != null); o.AcceptVisitor(this, data); } return null; }
public virtual object VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) { throw new global::System.NotImplementedException("MemberReferenceExpression"); }
public virtual object VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) { Debug.Assert((memberReferenceExpression != null)); Debug.Assert((memberReferenceExpression.TargetObject != null)); Debug.Assert((memberReferenceExpression.TypeArguments != null)); nodeStack.Push(memberReferenceExpression.TargetObject); memberReferenceExpression.TargetObject.AcceptVisitor(this, data); memberReferenceExpression.TargetObject = ((Expression)(nodeStack.Pop())); for (int i = 0; i < memberReferenceExpression.TypeArguments.Count; i++) { TypeReference o = memberReferenceExpression.TypeArguments[i]; Debug.Assert(o != null); nodeStack.Push(o); o.AcceptVisitor(this, data); o = (TypeReference)nodeStack.Pop(); if (o == null) memberReferenceExpression.TypeArguments.RemoveAt(i--); else memberReferenceExpression.TypeArguments[i] = o; } return null; }
void SimpleNonInvocationExpression(out Expression pexpr) { Expression expr; CollectionInitializerExpression cie; TypeReference type = null; string name = String.Empty; Location startLocation = la.Location; pexpr = null; if (StartOf(34)) { switch (la.kind) { case 3: { Get(); pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 4: { Get(); pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 7: { Get(); pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 6: { Get(); pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 5: { Get(); pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 9: { Get(); pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 8: { Get(); pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 217: { Get(); pexpr = new PrimitiveExpression(true, "true"); break; } case 122: { Get(); pexpr = new PrimitiveExpression(false, "false"); break; } case 165: { Get(); pexpr = new PrimitiveExpression(null, "null"); break; } case 37: { Get(); Expr(out expr); Expect(38); pexpr = new ParenthesizedExpression(expr); break; } case 2: case 58: case 62: case 64: case 65: case 66: case 67: case 70: case 87: case 98: case 104: case 107: case 116: case 121: case 126: case 133: case 139: case 143: case 146: case 147: case 170: case 176: case 178: case 184: case 203: case 212: case 213: case 223: case 224: case 230: { Identifier(); pexpr = new IdentifierExpression(t.val); pexpr.StartLocation = t.Location; pexpr.EndLocation = t.EndLocation; if (la.kind == Tokens.OpenParenthesis && Peek(1).kind == Tokens.Of) { Expect(37); Expect(169); TypeArgumentList(((IdentifierExpression)pexpr).TypeArguments); Expect(38); } break; } case 68: case 71: case 82: case 99: case 100: case 109: case 141: case 151: case 168: case 196: case 201: case 202: case 208: case 221: case 222: case 225: { string val = String.Empty; if (StartOf(13)) { PrimitiveTypeName(out val); } else { Get(); val = "System.Object"; } pexpr = new TypeReferenceExpression(new TypeReference(val, true)); break; } case 153: { Get(); pexpr = new ThisReferenceExpression(); break; } case 158: case 159: { Expression retExpr = null; if (la.kind == 158) { Get(); retExpr = new BaseReferenceExpression() { StartLocation = t.Location, EndLocation = t.EndLocation }; } else { Get(); retExpr = new ClassReferenceExpression() { StartLocation = t.Location, EndLocation = t.EndLocation }; } Expect(26); IdentifierOrKeyword(out name); pexpr = new MemberReferenceExpression(retExpr, name) { StartLocation = startLocation, EndLocation = t.EndLocation }; break; } case 130: { Get(); Expect(26); Identifier(); type = new TypeReference(t.val ?? ""); type.IsGlobal = true; pexpr = new TypeReferenceExpression(type); break; } case 162: { ObjectCreateExpression(out expr); pexpr = expr; break; } case 35: { CollectionInitializer(out cie); pexpr = cie; break; } case 94: case 106: case 219: { CastType castType = CastType.Cast; if (la.kind == 106) { Get(); } else if (la.kind == 94) { Get(); castType = CastType.Conversion; } else { Get(); castType = CastType.TryCast; } Expect(37); Expr(out expr); Expect(22); TypeName(out type); Expect(38); pexpr = new CastExpression(type, expr, castType); break; } case 76: case 77: case 78: case 79: case 80: case 81: case 83: case 85: case 86: case 90: case 91: case 92: case 93: case 95: case 96: case 97: { CastTarget(out type); Expect(37); Expr(out expr); Expect(38); pexpr = new CastExpression(type, expr, CastType.PrimitiveConversion); break; } case 57: { Get(); SimpleExpr(out expr); pexpr = new AddressOfExpression(expr); break; } case 129: { Get(); Expect(37); GetTypeTypeName(out type); Expect(38); pexpr = new TypeOfExpression(type); break; } case 220: { Get(); SimpleExpr(out expr); Expect(144); TypeName(out type); pexpr = new TypeOfIsExpression(expr, type); break; } case 135: { ConditionalExpression(out pexpr); break; } case 10: case 16: case 17: case 18: case 19: { XmlLiteralExpression(out pexpr); break; } } } else if (StartOf(35)) { if (la.kind == 26) { Get(); if (la.kind == 10) { Get(); IdentifierOrKeyword(out name); Expect(11); pexpr = new XmlMemberAccessExpression(null, XmlAxisType.Element, name, true) { StartLocation = startLocation, EndLocation = t.EndLocation }; } else if (StartOf(33)) { IdentifierOrKeyword(out name); pexpr = new MemberReferenceExpression(null, name) { StartLocation = startLocation, EndLocation = t.EndLocation }; } else SynErr(281); } else if (la.kind == 29) { Get(); IdentifierOrKeyword(out name); pexpr = new BinaryOperatorExpression(null, BinaryOperatorType.DictionaryAccess, new PrimitiveExpression(name, name) { StartLocation = t.Location, EndLocation = t.EndLocation }); } else { XmlAxisType axisType = XmlAxisType.Element; bool isXmlIdentifier = false; if (la.kind == 27) { Get(); axisType = XmlAxisType.Descendents; } else { Get(); axisType = XmlAxisType.Attribute; } if (la.kind == 10) { Get(); isXmlIdentifier = true; } IdentifierOrKeyword(out name); if (la.kind == 11) { Get(); } pexpr = new XmlMemberAccessExpression(null, axisType, name, isXmlIdentifier); } } else SynErr(282); if (pexpr != null) { pexpr.StartLocation = startLocation; pexpr.EndLocation = t.EndLocation; } }
void ReDimClauseInternal(ref Expression expr) { List<Expression> arguments; bool canBeNormal; bool canBeRedim; string name; Location startLocation = la.Location; while (la.kind == 26 || (la.kind == Tokens.OpenParenthesis && Peek(1).kind == Tokens.Of)) { if (la.kind == 26) { Get(); IdentifierOrKeyword(out name); expr = new MemberReferenceExpression(expr, name) { StartLocation = startLocation, EndLocation = t.EndLocation }; } else { InvocationExpression(ref expr); expr.StartLocation = startLocation; expr.EndLocation = t.EndLocation; } } Expect(37); NormalOrReDimArgumentList(out arguments, out canBeNormal, out canBeRedim); Expect(38); 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); } } }
public sealed override object VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) { this.BeginVisit(memberReferenceExpression); object result = this.TrackedVisitMemberReferenceExpression(memberReferenceExpression, data); this.EndVisit(memberReferenceExpression); return result; }
void SimpleExpr(out Expression pexpr) { string name; Location startLocation = la.Location; SimpleNonInvocationExpression(out pexpr); while (StartOf(32)) { if (la.kind == 26) { Get(); if (la.kind == 10) { Get(); IdentifierOrKeyword(out name); Expect(11); pexpr = new XmlMemberAccessExpression(pexpr, XmlAxisType.Element, name, true); } else if (StartOf(33)) { IdentifierOrKeyword(out name); pexpr = new MemberReferenceExpression(pexpr, name) { StartLocation = startLocation, EndLocation = t.EndLocation }; } else SynErr(280); if (la.kind == Tokens.OpenParenthesis && Peek(1).kind == Tokens.Of) { Expect(37); Expect(169); TypeArgumentList(((MemberReferenceExpression)pexpr).TypeArguments); Expect(38); } } else if (la.kind == 29) { Get(); IdentifierOrKeyword(out name); pexpr = new BinaryOperatorExpression(pexpr, BinaryOperatorType.DictionaryAccess, new PrimitiveExpression(name, name) { StartLocation = t.Location, EndLocation = t.EndLocation }); } else if (la.kind == 27 || la.kind == 28) { XmlAxisType type = XmlAxisType.Attribute; bool isXmlName = false; if (la.kind == 28) { Get(); } else { Get(); type = XmlAxisType.Descendents; } if (la.kind == 10) { Get(); isXmlName = true; } IdentifierOrKeyword(out name); if (la.kind == 11) { Get(); } pexpr = new XmlMemberAccessExpression(pexpr, type, name, isXmlName); } else { InvocationExpression(ref pexpr); } } if (pexpr != null) { pexpr.StartLocation = startLocation; pexpr.EndLocation = t.EndLocation; } }
public virtual object TrackedVisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) { return base.VisitMemberReferenceExpression(memberReferenceExpression, data); }
static CodeTypeReferenceExpression ConvertToTypeReference(MemberReferenceExpression fieldReferenceExpression) { StringBuilder type = new StringBuilder(""); while (fieldReferenceExpression.TargetObject is MemberReferenceExpression) { type.Insert(0,'.'); type.Insert(1,fieldReferenceExpression.MemberName.ToCharArray()); fieldReferenceExpression = (MemberReferenceExpression)fieldReferenceExpression.TargetObject; } type.Insert(0,'.'); type.Insert(1,fieldReferenceExpression.MemberName.ToCharArray()); if (fieldReferenceExpression.TargetObject is IdentifierExpression) { type.Insert(0, ((IdentifierExpression)fieldReferenceExpression.TargetObject).Identifier.ToCharArray()); string oldType = type.ToString(); int idx = oldType.LastIndexOf('.'); while (idx > 0) { if (Type.GetType(type.ToString()) != null) { break; } string stype = type.ToString().Substring(idx + 1); type = new StringBuilder(type.ToString().Substring(0, idx)); type.Append("+"); type.Append(stype); idx = type.ToString().LastIndexOf('.'); } if (Type.GetType(type.ToString()) == null) { type = new StringBuilder(oldType); } return new CodeTypeReferenceExpression(type.ToString()); } else if (fieldReferenceExpression.TargetObject is TypeReferenceExpression) { type.Insert(0, ((TypeReferenceExpression)fieldReferenceExpression.TargetObject).TypeReference.Type); return new CodeTypeReferenceExpression(type.ToString()); } else { return null; } }
bool IsPossibleTypeReference(MemberReferenceExpression fieldReferenceExpression) { while (fieldReferenceExpression.TargetObject is MemberReferenceExpression) { fieldReferenceExpression = (MemberReferenceExpression)fieldReferenceExpression.TargetObject; } IdentifierExpression identifier = fieldReferenceExpression.TargetObject as IdentifierExpression; if (identifier != null) return !IsField(identifier.Identifier) && !IsLocalVariable(identifier.Identifier); TypeReferenceExpression tre = fieldReferenceExpression.TargetObject as TypeReferenceExpression; if (tre != null) return true; return false; }
public override object VisitMemberReferenceExpression(MemberReferenceExpression fieldReferenceExpression, object data) { if (methodReference) { methodReference = false; return new CodeMethodReferenceExpression((CodeExpression)fieldReferenceExpression.TargetObject.AcceptVisitor(this, data), fieldReferenceExpression.MemberName); } if (IsFieldReferenceExpression(fieldReferenceExpression)) { return new CodeFieldReferenceExpression((CodeExpression)fieldReferenceExpression.TargetObject.AcceptVisitor(this, data), fieldReferenceExpression.MemberName); } else { if (fieldReferenceExpression.TargetObject is MemberReferenceExpression) { if (IsPossibleTypeReference((MemberReferenceExpression)fieldReferenceExpression.TargetObject)) { CodeTypeReferenceExpression typeRef = ConvertToTypeReference((MemberReferenceExpression)fieldReferenceExpression.TargetObject); if (IsField(typeRef.Type.BaseType, fieldReferenceExpression.MemberName)) { return new CodeFieldReferenceExpression(typeRef, fieldReferenceExpression.MemberName); } else { return new CodePropertyReferenceExpression(typeRef, fieldReferenceExpression.MemberName); } } } CodeExpression codeExpression = (CodeExpression)fieldReferenceExpression.TargetObject.AcceptVisitor(this, data); return new CodePropertyReferenceExpression(codeExpression, fieldReferenceExpression.MemberName); } }
bool IsFieldReferenceExpression(MemberReferenceExpression fieldReferenceExpression) { if (fieldReferenceExpression.TargetObject is ThisReferenceExpression || fieldReferenceExpression.TargetObject is BaseReferenceExpression) { //field detection for fields\props inherited from base classes return IsField(fieldReferenceExpression.MemberName); } return false; }