public VisitedEventArgs(ILTranslationContext context, T node, IEnumerable <Syntax> results) { Context = context; Node = node; var c = (results as ICollection <Syntax>) ?? results.ToArray(); Results = (c as IList <Syntax>) ?? c.ToList(); _OriginalResults = Results; }
protected virtual D.IModuleDeclaration ResolveModule(ILTranslationContext data, string fullName) { var ms = Statements; var m = ms.OfType <D.IModuleDeclaration>().FirstOrDefault(e => e.Name == fullName); if (m == null) { m = new D.NamespaceDeclaration() { Name = fullName }; ms.Add(m); } return(m); }
private IEnumerable <D.Parameter> GetParameters(ILTranslationContext data, AstNodeCollection <ParameterDeclaration> parameters) { foreach (var p in parameters) { var dp = new D.Parameter() { ParameterName = p.Name, }; dp.ParameterType = ResolveType(p, p.Type); dp.Decorators = GetDecorators(p.Attributes, data); if (p.DefaultExpression?.IsNull == false) { dp.Initializer = (Expression)p.DefaultExpression.AcceptVisitor(this, data).LastOrDefault(); } dp.IsOptional = dp.Initializer != null; yield return(dp); } }
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitTypeDeclaration(TypeDeclaration typeDeclaration, ILTranslationContext data) => OnVisiting(data, typeDeclaration, VisitingTypeDeclaration) ?? OnVisited(data, typeDeclaration, VisitedTypeDeclaration, TranslateTypeDeclaration(typeDeclaration, data));
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitPreProcessorDirective(PreProcessorDirective preProcessorDirective, ILTranslationContext data) => Enumerable.Empty <Syntax>();
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration, ILTranslationContext data) => OnVisiting(data, namespaceDeclaration, VisitingNamespaceDeclaration) ?? OnVisited(data, namespaceDeclaration, VisitedNamespaceDeclaration, TranslateNamespaceDeclaration(namespaceDeclaration, data));
protected virtual IEnumerable <Syntax> TranslateAttribute(Attribute attribute, ILTranslationContext data) { var d = new D.Decorator(); d.Name = GetTypeName(attribute.Type); foreach (var c in attribute.Children) { if (c is SimpleType) { continue; } else { foreach (var cr in c.AcceptVisitor(this, data)) { d.Parameters.Add((Expression)cr); } } } yield return(d); }
protected virtual IEnumerable <Syntax> TranslateAttributeSection(AttributeSection attributeSection, ILTranslationContext data) { foreach (var c in attributeSection.Children) { foreach (var cr in c.AcceptVisitor(this, data)) { yield return(cr); } } }
protected virtual IEnumerable <Syntax> TranslateIndexerExpression(IndexerExpression indexerExpression, ILTranslationContext data) { yield return(new E.IndexerExpression() { Object = GetExpression(indexerExpression.Target, data), Index = GetExpression(indexerExpression.Arguments.Single(), data), }); }
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitIndexerExpression(IndexerExpression indexerExpression, ILTranslationContext data) => OnVisiting(data, indexerExpression, VisitingIndexerExpression) ?? OnVisited(data, indexerExpression, VisitedIndexerExpression, TranslateIndexerExpression(indexerExpression, data));
protected virtual E.CallExpression TranslateInvocationExpression(InvocationExpression invocationExpression, ILTranslationContext data) { var inv = new E.CallExpression(); var mre = invocationExpression.Target as MemberReferenceExpression; if (mre != null) { // TODO: method mapping foreach (var t in mre.TypeArguments) { inv.TypeArguments.Add(ResolveType(mre, t)); } inv.Target = new E.PropertyExpression() { Object = GetExpression(mre.Target, data), Property = mre.MemberName }; } else { inv.Target = GetExpression(invocationExpression.Target, data); } foreach (var p in invocationExpression.Arguments) { inv.Parameters.Add(GetExpression(p, data)); } return(inv); }
protected virtual IEnumerable <Syntax> TranslateUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, ILTranslationContext data) { if (unaryOperatorExpression.Operator == UnaryOperatorType.Await) { data.HasAwait = true; return(new[] { new E.AwaitExpression() { Operand = GetExpression(unaryOperatorExpression.Expression, data) } }); } var e = new E.UnaryExpression(); e.Operand = GetExpression(unaryOperatorExpression.Expression, data); switch (unaryOperatorExpression.Operator) { case UnaryOperatorType.Plus: e.Operator = E.UnaryOperator.Plus; break; case UnaryOperatorType.Minus: e.Operator = E.UnaryOperator.Minus; break; case UnaryOperatorType.Increment: e.Operator = E.UnaryOperator.PrefixIncrement; break; case UnaryOperatorType.PostIncrement: e.Operator = E.UnaryOperator.PostfixIncrement; break; case UnaryOperatorType.Decrement: e.Operator = E.UnaryOperator.PrefixDecrement; break; case UnaryOperatorType.PostDecrement: e.Operator = E.UnaryOperator.PostfixDecrement; break; case UnaryOperatorType.Not: e.Operator = E.UnaryOperator.LogicalNot; break; case UnaryOperatorType.BitNot: e.Operator = E.UnaryOperator.BitwiseNot; break; default: throw GetNotImplementedException(); } return(new[] { e }); }
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, ILTranslationContext data) => OnVisiting(data, unaryOperatorExpression, VisitingUnaryOperatorExpression) ?? OnVisited(data, unaryOperatorExpression, VisitedUnaryOperatorExpression, TranslateUnaryOperatorExpression(unaryOperatorExpression, data));
protected virtual IEnumerable <Syntax> TranslateParenthesizedExpression(ParenthesizedExpression parenthesizedExpression, ILTranslationContext data) => parenthesizedExpression.Expression.AcceptVisitor(this, data);
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitParenthesizedExpression(ParenthesizedExpression parenthesizedExpression, ILTranslationContext data) => OnVisiting(data, parenthesizedExpression, VisitingParenthesizedExpression) ?? OnVisited(data, parenthesizedExpression, VisitedParenthesizedExpression, TranslateParenthesizedExpression(parenthesizedExpression, data));
protected virtual IEnumerable <Syntax> TranslateMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, ILTranslationContext data) { if (memberReferenceExpression.TypeArguments.Any()) { throw GetNotImplementedException(); } yield return(new E.PropertyExpression() { Object = GetExpression(memberReferenceExpression.Target, data), Property = memberReferenceExpression.MemberName }); }
private Expression GetExpression(ICSharpCode.NRefactory.CSharp.Expression expression, ILTranslationContext data) => expression?.IsNull != false ? null : expression.AcceptVisitor(this, data).Cast <Expression>().Single();
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, ILTranslationContext data) => OnVisiting(data, objectCreateExpression, VisitingObjectCreateExpression) ?? OnVisited(data, objectCreateExpression, VisitedObjectCreateExpression, TranslateObjectCreateExpression(objectCreateExpression, data));
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitAttributeSection(AttributeSection attributeSection, ILTranslationContext data) => OnVisiting(data, attributeSection, VisitingAttributeSection) ?? OnVisited(data, attributeSection, VisitedAttributeSection, TranslateAttributeSection(attributeSection, data));
protected virtual IEnumerable <Syntax> TranslateObjectCreateExpression(ObjectCreateExpression objectCreateExpression, ILTranslationContext data) { E.NewExpression ne; var clrType = objectCreateExpression.Type.ResolveClrType(); if (clrType != null) { if (typeof(MulticastDelegate).IsAssignableFrom(clrType)) { var p = objectCreateExpression.Arguments.OfType <MemberReferenceExpression>().FirstOrDefault(); if (p != null) { var t = p.Target.AcceptVisitor(this, data).ToArray(); if (t.Length == 1) { var te = (Expression)t[0]; yield return(te.Property(p.MemberName).Property("bind").Call(te)); yield break; } } } ne = new E.NewExpression(); ne.Type = ResolveClrType(objectCreateExpression, clrType).ToExpression(); } else { ne = new E.NewExpression(); ne.Type = ResolveType(objectCreateExpression, objectCreateExpression.Type).ToExpression(); } foreach (var p in objectCreateExpression.Arguments) { ne.Parameters.Add(p.AcceptVisitor(this, data).Cast <Expression>().Single()); } // TODO: initializer yield return(ne); }
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitAttribute(Attribute attribute, ILTranslationContext data) => OnVisiting(data, attribute, VisitingAttribute) ?? OnVisited(data, attribute, VisitedAttribute, TranslateAttribute(attribute, data));
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, ILTranslationContext data) => OnVisiting(data, binaryOperatorExpression, VisitingBinaryOperatorExpression) ?? OnVisited(data, binaryOperatorExpression, VisitedBinaryOperatorExpression, TranslateBinaryOperatorExpression(binaryOperatorExpression, data));
private Collection <D.Decorator> GetDecorators(IEnumerable <AttributeSection> section, ILTranslationContext data) { Collection <D.Decorator> c = null; foreach (var s in section) { foreach (var a in s.Attributes) { foreach (D.Decorator d in a.AcceptVisitor(this, data)) { (c ?? (c = new Collection <D.Decorator>())).Add(d); } } } return(c); }
protected virtual IEnumerable <Syntax> TranslateBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, ILTranslationContext data) { yield return(new E.BinaryExpression() { Left = GetExpression(binaryOperatorExpression.Left, data), Right = GetExpression(binaryOperatorExpression.Right, data), Operator = GetOperator(binaryOperatorExpression.Operator) }); }
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitUsingDeclaration(UsingDeclaration usingDeclaration, ILTranslationContext data) => Enumerable.Empty <Syntax>();
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitAssignmentExpression(AssignmentExpression assignmentExpression, ILTranslationContext data) => OnVisiting(data, assignmentExpression, VisitingAssignmentExpression) ?? OnVisited(data, assignmentExpression, VisitedAssignmentExpression, TranslateAssignmentExpression(assignmentExpression, data));
protected virtual IEnumerable <Syntax> TranslateNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration, ILTranslationContext data) { var ns = ResolveModule(data, namespaceDeclaration.FullName); foreach (var c in namespaceDeclaration.Children) { if (c is MemberType) { continue; } if (c is NamespaceDeclaration) { foreach (var cr in c.AcceptVisitor(this, data)) { yield return(cr); } } else { foreach (var cr in c.AcceptVisitor(this, data)) { ns.Members.Add(cr); } } } yield return((Syntax)ns); }
protected virtual IEnumerable <Syntax> TranslateAssignmentExpression(AssignmentExpression assignmentExpression, ILTranslationContext data) { yield return(new E.AssignmentExpression() { Target = GetExpression(assignmentExpression.Left, data), Value = GetExpression(assignmentExpression.Right, data), CompoundOperator = assignmentExpression.Operator == AssignmentOperatorType.Assign ? E.BinaryOperator.Default : GetOperator(assignmentExpression.Operator) }); }
protected virtual IEnumerable <Syntax> TranslateTypeDeclaration(TypeDeclaration typeDeclaration, ILTranslationContext data) { D.ITypeDeclaration td; switch (typeDeclaration.ClassType) { case ClassType.Class: { var cd = new D.ClassDeclaration() { IsAbstract = typeDeclaration.HasModifier(Modifiers.Abstract) }; td = cd; foreach (var p in GetTypeParameters(typeDeclaration.TypeParameters, typeDeclaration.Constraints)) { cd.TypeParameters.Add(p); } } break; case ClassType.Struct: { var cd = new D.ClassDeclaration(); td = cd; foreach (var p in GetTypeParameters(typeDeclaration.TypeParameters, typeDeclaration.Constraints)) { cd.TypeParameters.Add(p); } } break; case ClassType.Interface: var id = new D.InterfaceDeclaration(); td = id; foreach (var p in GetTypeParameters(typeDeclaration.TypeParameters, typeDeclaration.Constraints)) { id.TypeParameters.Add(p); } break; case ClassType.Enum: td = new D.EnumDeclaration(); break; default: throw GetNotImplementedException(); } var decType = typeDeclaration.Parent as TypeDeclaration; td.Name = typeDeclaration.Name; td.IsExport = typeDeclaration.HasModifier(Modifiers.Public); while (decType != null) { td.Name = decType.Name + "$" + td.Name; td.IsExport &= decType.HasModifier(Modifiers.Public); decType = decType.Parent as TypeDeclaration; } foreach (var bt in typeDeclaration.BaseTypes) { var dt = ResolveType(typeDeclaration, bt); var cd = td as D.ClassDeclaration; if (cd != null && cd.BaseType == null && dt.IsClass == true) { cd.BaseType = dt; } else if (cd != null) { cd.Interfaces.Add(dt); } else { ((D.InterfaceDeclaration)td).BaseTypes.Add(dt); } } foreach (var c in typeDeclaration.Attributes) { foreach (var cr in c.AcceptVisitor(this, data)) { td.Decorators.Add((D.Decorator)cr); } } foreach (var c in typeDeclaration.Members) { var ctd = c as TypeDeclaration; if (ctd != null) { foreach (var cr in c.AcceptVisitor(this, data)) { yield return(cr); } continue; } foreach (var cr in c.AcceptVisitor(this, data)) { td.Members.Add(cr); } } yield return((Syntax)td); }
IEnumerable <Syntax> IAstVisitor <ILTranslationContext, IEnumerable <Syntax> > .VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, ILTranslationContext data) => OnVisiting(data, memberReferenceExpression, VisitingMemberReferenceExpression) ?? OnVisited(data, memberReferenceExpression, VisitedMemberReferenceExpression, TranslateMemberReferenceExpression(memberReferenceExpression, data));