Exemple #1
0
        private CtorMethod TranslateCtor(ConstructorDeclarationSyntax node, CSharpSyntaxVisitor <IStmt> statementVisitor)
        {
            var methodSymbol = semanticModel.GetDeclaredSymbol(node);
            BaseCtorCallExpr baseCtorCall = null;

            if (node.Initializer != null)
            {
                baseCtorCall = new BaseCtorCallExpr
                {
                    Arguments = node.Initializer.ArgumentList.Arguments.Select(statementVisitor.Visit).ToArray()
                };
            }
            return(new CtorMethod
            {
                Name = methodSymbol.ReceiverType.Name,
                Parameters = methodSymbol.Parameters.Select(parameterSymbol => new MethodParameterInfo
                {
                    Name = parameterSymbol.Name,
                    DefaultValue = TranslatorHelper.GetParameterDefaultValue(parameterSymbol),
                    ParameterSymbol = parameterSymbol
                }).ToArray(),
                Body = statementVisitor.Visit(node.Body),
                MethodSymbol = methodSymbol,
                BaseCtorCallExpr = baseCtorCall,
                DeclaredAccessibility = methodSymbol.DeclaredAccessibility
            });
        }
Exemple #2
0
        public JavaClass Translate(ClassDeclarationSyntax declarationNode, CSharpSyntaxVisitor <IStmt> statementTranslator)
        {
            var symbol = semanticModel.GetDeclaredSymbol(declarationNode);

            if (symbol == null)
            {
                throw new Exception("Cannot build semantic information for class type");
            }
            var descendantNodes = declarationNode.DescendantNodes().ToArray();
            var methods         = descendantNodes.OfType <MethodDeclarationSyntax>()
                                  .Select(method => TranslatorHelper.TranslateMethod(semanticModel, method, statementTranslator)).ToArray();
            var ctors  = descendantNodes.OfType <ConstructorDeclarationSyntax>().Select(ctor => TranslateCtor(ctor, statementTranslator)).ToArray();
            var fields = TranslatorHelper.GetFields(declarationNode)
                         .Select(node => TranslatorHelper.TranslateField(semanticModel, node, statementTranslator));
            var props = TranslatorHelper.GetProperties(declarationNode)
                        .Select(node => TranslatorHelper.TranslateProp(semanticModel, node, statementTranslator));
            var className = declarationNode.Identifier.ToString();

            return(new JavaClass
            {
                Name = className,
                Methods = ctors.Concat <IMethod>(methods).ToArray(),
                Fields = props.Concat(fields).ToArray(),
                TypeSymbol = symbol,
                DeclaredAccessibility = symbol.DeclaredAccessibility,
                IsAbstract = symbol.IsAbstract
            });
        }
Exemple #3
0
 private static void VisitDeclaringReferences(INamedTypeSymbol namedType, CSharpSyntaxVisitor visitor)
 {
     foreach (var reference in namedType.DeclaringSyntaxReferences)
     {
         visitor.Visit(reference.GetSyntax());
     }
 }
Exemple #4
0
        public TNode IncrementalVisitSeparatedSyntaxList <TNode, TElement>(
            TNode node,
            Func <TNode, SeparatedSyntaxList <TElement> > getList,
            Func <TNode, SeparatedSyntaxList <TElement>, TNode> withList,
            CSharpSyntaxVisitor <SyntaxNode> elementVisitor  = null,
            Func <SyntaxToken, SyntaxToken> separatorVisitor = null)
            where TNode : SyntaxNode
            where TElement : SyntaxNode
        {
            elementVisitor   = elementVisitor ?? this;
            separatorVisitor = separatorVisitor ?? this.cachedVisitTokenFunc;

            var count = getList(node).GetWithSeparators().Count;

            for (var i = 0; i < count; ++i)
            {
                var list    = getList(node).GetWithSeparators();
                var element = list[i];
                var visited = element.IsNode
                    ? elementVisitor.TypedVisit(element.AsNode())
                    : (SyntaxNodeOrToken)separatorVisitor(element.AsToken());
                node = withList(node, SyntaxFactory.SeparatedList <TElement>(list.Replace(element, visited)));
            }

            return(node);
        }
Exemple #5
0
 public CommonConversions(SemanticModel semanticModel, CSharpSyntaxVisitor <VisualBasicSyntaxNode> nodesVisitor,
                          TriviaConverter triviaConverter)
 {
     _semanticModel   = semanticModel;
     _nodesVisitor    = nodesVisitor;
     _triviaConverter = triviaConverter;
 }
Exemple #6
0
 public static void AcceptAll <T>(this IEnumerable <T> source, CSharpSyntaxVisitor visitor)
     where T : CSharpSyntaxNode
 {
     foreach (var item in source)
     {
         item.Accept(visitor);
     }
 }
Exemple #7
0
 public MethodBodyExecutableStatementVisitor(SemanticModel semanticModel,
                                             CSharpSyntaxVisitor <VisualBasicSyntaxNode> nodesVisitor, TriviaConverter triviaConverter,
                                             CommonConversions commonConversions)
 {
     this._semanticModel      = semanticModel;
     this._nodesVisitor       = nodesVisitor;
     _commonConversions       = commonConversions;
     CommentConvertingVisitor = new CommentConvertingMethodBodyVisitor(this, triviaConverter);
 }
Exemple #8
0
        public SyntaxNode VisitInvocation(InvocationExpressionSyntax node, IMethodSymbol symbol,
                                          CSharpSyntaxVisitor <SyntaxNode> visitor)
        {
            //先将范型参数转换为模型Id
            ServiceCodeGenerator generator = (ServiceCodeGenerator)visitor;

            string appName;
            string modelTypeName;
            string loadMethod = null;
            bool   isSysStore = symbol.IsGenericMethod;

            if (isSysStore) //是系统内置数据库的加载方法
            {
                appName       = symbol.TypeArguments[0].ContainingNamespace.ContainingNamespace.Name;
                modelTypeName = symbol.TypeArguments[0].Name;
                loadMethod    = "appbox.Store.EntityStore.LoadAsync";
            }
            else //SqlStore的加载方法
            {
                appName       = symbol.ContainingType.ContainingNamespace.ContainingNamespace.Name;
                modelTypeName = symbol.ContainingType.Name;
            }

            var appNode   = generator.hub.DesignTree.FindApplicationNodeByName(appName);
            var modelNode = generator.hub.DesignTree.FindModelNodeByName(appNode.Model.Id, ModelType.Entity, modelTypeName);
            var model     = (EntityModel)modelNode.Model;

            if (!isSysStore)
            {
                loadMethod = $"appbox.Store.SqlStore.Get({model.SqlStoreOptions.StoreModelId}ul).LoadAsync";
            }
            var exp = SyntaxFactory.ParseExpression(loadMethod);

            var modelIdArg = SyntaxFactory.Argument(
                SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(modelNode.Model.Id)));
            var args = SyntaxFactory.ArgumentList().AddArguments(modelIdArg);

            foreach (var oldArg in node.ArgumentList.Arguments)
            {
                args = args.AddArguments((ArgumentSyntax)visitor.Visit(oldArg));
            }

            var res = SyntaxFactory.InvocationExpression(exp, args).WithTriviaFrom(node);

            return(res);
        }
Exemple #9
0
        public TNode IncrementalVisitSyntaxList <TNode, TElement>(
            TNode node,
            Func <TNode, SyntaxList <TElement> > getList,
            Func <TNode, SyntaxList <TElement>, TNode> withList,
            CSharpSyntaxVisitor <SyntaxNode> elementVisitor = null)
            where TNode : SyntaxNode
            where TElement : SyntaxNode
        {
            elementVisitor = elementVisitor ?? this;

            var count = getList(node).Count;

            for (var i = 0; i < count; ++i)
            {
                var list    = getList(node);
                var element = list[i];
                var visited = elementVisitor.TypedVisit(element);
                node = withList(node, list.Replace(element, visited));
            }

            return(node);
        }
Exemple #10
0
        public SyntaxNode VisitInvocation(InvocationExpressionSyntax node,
                                          IMethodSymbol symbol, CSharpSyntaxVisitor <SyntaxNode> visitor)
        {
            //将旧方法转换为服务名称参数
            var service = $"{symbol.ContainingType.ContainingNamespace.ContainingNamespace}.{symbol.ContainingType.Name}.{symbol.Name}";

            var returnType = symbol.ReturnType as INamedTypeSymbol;
            var method     = SyntaxFactory.ParseExpression(GetMethodByReturnType(returnType))
                             .WithTriviaFrom(node.Expression);

            //添加服务名称参数
            var serviceArg = SyntaxFactory.Argument(
                SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression,
                                                SyntaxFactory.Literal(service)));
            var args = SyntaxFactory.ArgumentList().AddArguments(serviceArg);

            //转换旧有参数(如果有)
            args = MakeInvokeArgs(node, visitor, args).WithTriviaFrom(node.ArgumentList);

            var res = SyntaxFactory.InvocationExpression(method, args).WithTriviaFrom(node);

            return(res);
        }
Exemple #11
0
 public static void Accept(this SyntaxNode n, CSharpSyntaxVisitor vis)
 {
     (n as CSharpSyntaxNode).Accept(vis);
 }
Exemple #12
0
        public IStmt Translate(InvocationExpressionSyntax expression, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
        {
            var symbol = semanticModel.GetSymbolInfo(expression).Symbol;

            if (symbol == null || !SymbolHelper.IsAssignableFrom(symbol, "Stack"))
            {
                return(null);
            }
            var arguments       = expression.ArgumentList.Arguments;
            var exactSignatures = new Dictionary <string, string>
            {
                { "Push", "push" },
                { "Pop", "pop" },
            };

            if (exactSignatures.ContainsKey(symbol.Name))
            {
                var javaMethod = exactSignatures[symbol.Name];
                return(new CallExpr
                {
                    Expression = new MemberAccessExpr
                    {
                        ObjectExpr = visitor.Visit(ExtensionPointHelper.TryGetMemberOwner(expression.Expression)),
                        MemberExpr = new IdentifierExpr {
                            Identifier = javaMethod
                        }
                    },
                    Arguments = arguments.Select(visitor.Visit).ToArray()
                });
            }
            return(null);
        }
 public CommentConvertingVisitorWrapper(CSharpSyntaxVisitor <T> wrappedVisitor)
 {
     _wrappedVisitor = wrappedVisitor;
 }
Exemple #14
0
        public SyntaxNode VisitInvocation(InvocationExpressionSyntax node, IMethodSymbol symbol, CSharpSyntaxVisitor <SyntaxNode> visitor)
        {
            //转换方法名称
            var oldMemExp = (MemberAccessExpressionSyntax)node.Expression;
            var newName   = (SimpleNameSyntax)SyntaxFactory.ParseName("Where");
            var newMemExp = SyntaxFactory.MemberAccessExpression(
                SyntaxKind.SimpleMemberAccessExpression, oldMemExp.Expression, newName);

            //处理参数转换为KeyPredicate
            var target = node.ArgumentList.Arguments[0].Expression;
            MemberAccessExpressionSyntax memberAccess = null;

            if (target is SimpleLambdaExpressionSyntax)
            {
                memberAccess = ((SimpleLambdaExpressionSyntax)target).Body as MemberAccessExpressionSyntax;
            }
            else if (target is ParenthesizedLambdaExpressionSyntax)
            {
                memberAccess = ((ParenthesizedLambdaExpressionSyntax)target).Body as MemberAccessExpressionSyntax;
            }
            if (memberAccess == null)
            {
                throw new ArgumentException("PartitionPredicate参数错误");
            }

            var valueExp = node.ArgumentList.Arguments[1].Expression;

            valueExp = (ExpressionSyntax)valueExp.Accept(visitor);

            ServiceCodeGenerator generator = (ServiceCodeGenerator)visitor;
            var    entitySymbol            = generator.SemanticModel.GetSymbolInfo(memberAccess.Expression).Symbol;
            var    names           = entitySymbol.ToString().Split('.');
            var    appNode         = generator.hub.DesignTree.FindApplicationNodeByName(names[0]);
            var    entityModelNode = generator.hub.DesignTree.FindModelNodeByName(appNode.Model.Id, ModelType.Entity, names[2]);
            var    entityModel     = (EntityModel)entityModelNode.Model;
            ushort memberId        = 0;

            if (memberAccess.Name.Identifier.Text != nameof(Data.Entity.CreateTime)) //注意排除特殊成员
            {
                memberId = entityModel.GetMember(memberAccess.Name.Identifier.Text, true).MemberId;
            }

            //判断成员是否分区键
            if (entityModel.SysStoreOptions.PartitionKeys == null)
            {
                throw new Exception("非分区表不能指定分区谓词");
            }
            int pkIndex = Array.FindIndex(entityModel.SysStoreOptions.PartitionKeys, t => t.MemberId == memberId);

            if (pkIndex == -1)
            {
                throw new Exception("指定成员非分区键");
            }

            var arg1Exp = SyntaxFactory.ParseExpression($"new appbox.Store.KeyPredicate({memberId}, appbox.Store.KeyPredicateType.{oldMemExp.Name}, {valueExp})");
            var arg1    = SyntaxFactory.Argument(arg1Exp);
            var arg2Exp = SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression,
                                                          SyntaxFactory.Literal(pkIndex));
            var arg2    = SyntaxFactory.Argument(arg2Exp);
            var arg3Exp = SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression,
                                                          SyntaxFactory.Literal(entityModel.SysStoreOptions.PartitionKeys.Length));
            var arg3 = SyntaxFactory.Argument(arg3Exp);

            var argList = SyntaxFactory.ArgumentList().AddArguments(arg1, arg2, arg3);

            var res = SyntaxFactory.InvocationExpression(newMemExp, argList);

            return(res.WithTriviaFrom(node));
        }
Exemple #15
0
 public CommentConvertingNodesVisitor(CSharpSyntaxVisitor <VisualBasicSyntaxNode> wrappedVisitor)
 {
     _wrappedVisitor = wrappedVisitor;
 }
        public IStmt Translate(MemberAccessExpressionSyntax expression, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
        {
            var symbol = GetArraySymbol(semanticModel, expression);

            if (symbol == null)
            {
                return(null);
            }
            var mapping = new Dictionary <string, string>
            {
                { "Length", "length" }
            };

            if (mapping.ContainsKey(symbol.Name))
            {
                return(new MemberAccessExpr
                {
                    ObjectExpr = visitor.Visit(expression.Expression),
                    MemberExpr = new IdentifierExpr {
                        Identifier = mapping[symbol.Name]
                    }
                });
            }
            return(null);
        }
Exemple #17
0
        public static JavaMethod TranslateMethod(SemanticModel semanticModel, MethodDeclarationSyntax node, CSharpSyntaxVisitor <IStmt> statementVisitor)
        {
            var methodSymbol = semanticModel.GetDeclaredSymbol(node);
            var isStatic     = methodSymbol.IsStatic;
            var body         = methodSymbol.IsAbstract
                ? new CompoundStmt {
                Statements = new IStmt[0]
            }
                : statementVisitor.Visit(node.Body);

            return(new JavaMethod
            {
                Name = methodSymbol.Name,
                Parameters = methodSymbol.Parameters.Select(parameterSymbol => new MethodParameterInfo
                {
                    Name = parameterSymbol.Name,
                    DefaultValue = GetParameterDefaultValue(parameterSymbol),
                    ParameterSymbol = parameterSymbol
                }).ToArray(),
                Body = body,
                IsStatic = isStatic,
                IsAbstract = methodSymbol.IsAbstract,
                MethodSymbol = methodSymbol,
                DeclaredAccessibility = methodSymbol.DeclaredAccessibility
            });
        }
Exemple #18
0
        public SyntaxNode VisitInvocation(InvocationExpressionSyntax node, IMethodSymbol symbol, CSharpSyntaxVisitor <SyntaxNode> visitor)
        {
            var target = node.ArgumentList.Arguments[0].Expression;
            MemberAccessExpressionSyntax memberAccess = null;

            if (target is SimpleLambdaExpressionSyntax)
            {
                memberAccess = ((SimpleLambdaExpressionSyntax)target).Body as MemberAccessExpressionSyntax;
            }
            else if (target is ParenthesizedLambdaExpressionSyntax)
            {
                memberAccess = ((ParenthesizedLambdaExpressionSyntax)target).Body as MemberAccessExpressionSyntax;
            }

            if (memberAccess == null)
            {
                throw new ArgumentException("Include参数错误");
            }

            ServiceCodeGenerator generator = (ServiceCodeGenerator)visitor;

            if (!(generator.SemanticModel.GetSymbolInfo(memberAccess).Symbol is IPropertySymbol memberSymbol))
            {
                throw new ArgumentException("Include参数错误");
            }

            if (TypeHelper.IsEntityClass(memberSymbol.Type as INamedTypeSymbol))
            {
                throw new NotImplementedException();
            }
            else if (memberSymbol.Type.ToString().StartsWith(TypeHelper.Type_EntityList, StringComparison.Ordinal)) //TODO:暂简单判断
            {
                throw new NotImplementedException();
            }
            else //Include(t => t.Customer.Name)
            {
                //先判断不允许t => t.Name
                if (memberAccess.Expression is IdentifierNameSyntax)
                {
                    throw new Exception("不允许Include如t=>t.Name");
                }

                var levels = new List <ValueTuple <string, ushort> >();
                PathToLevels(generator, memberAccess, memberSymbol, levels);
                levels.Reverse();
                var aliasName = string.Concat(levels.Select(t => t.Item1));

                var argsArray = new ArgumentSyntax[levels.Count + 1];
                argsArray[0] = SyntaxFactory.Argument(
                    SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression,
                                                    SyntaxFactory.Literal(aliasName)));
                for (int i = 0; i < levels.Count; i++)
                {
                    argsArray[i + 1] = SyntaxFactory.Argument(
                        SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression,
                                                        SyntaxFactory.Literal(levels[i].Item2)));
                }
                var args = SyntaxFactory.ArgumentList().AddArguments(argsArray);
                return(node.WithArgumentList(args));
            }
        }
 public override void Accept(CSharpSyntaxVisitor visitor)
 {
     throw new NotImplementedException();
 }
Exemple #20
0
        private SyntaxList <StatementSyntax> ConvertStatement(Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax statement, CSharpSyntaxVisitor <SyntaxList <StatementSyntax> > methodBodyVisitor)
        {
            var convertedStatements = statement.Accept(methodBodyVisitor);

            convertedStatements = InsertRequiredDeclarations(convertedStatements, statement);

            return(convertedStatements);
        }
Exemple #21
0
        public IStmt Translate(MemberAccessExpressionSyntax expression, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
        {
            var symbol = GetStringSymbol(semanticModel, expression);

            if (symbol == null)
            {
                return(null);
            }
            var memberToMethodMappings = new Dictionary <string, string>
            {
                { "Length", "length" }
            };

            if (memberToMethodMappings.ContainsKey(symbol.Name))
            {
                var targetMethod = memberToMethodMappings[symbol.Name];
                return(new CallExpr
                {
                    Expression = new MemberAccessExpr
                    {
                        ObjectExpr = visitor.Visit(ExtensionPointHelper.TryGetMemberOwner(expression)),
                        MemberExpr = new IdentifierExpr {
                            Identifier = targetMethod
                        }
                    },
                    Arguments = new IStmt[0]
                });
            }
            return(null);
        }
Exemple #22
0
        public IStmt Translate(InvocationExpressionSyntax expression, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
        {
            var symbol = GetStringSymbol(semanticModel, expression);

            if (symbol == null)
            {
                return(null);
            }
            var arguments       = expression.ArgumentList.Arguments;
            var exactSignatures = new Dictionary <string, string>
            {
                { "ToLower", "toLowerCase" },
                { "ToUpper", "toUpperCase" },
                { "EndsWith", "endsWith" },
                { "StartsWith", "startsWith" },
                { "IndexOf", "indexOf" },
                { "Substring", "substring" },
                { "Contains", "contains" }
            };

            if (exactSignatures.ContainsKey(symbol.Name))
            {
                var javaMethod = exactSignatures[symbol.Name];
                return(new CallExpr
                {
                    Expression = new MemberAccessExpr
                    {
                        ObjectExpr = visitor.Visit(ExtensionPointHelper.TryGetMemberOwner(expression.Expression)),
                        MemberExpr = new IdentifierExpr {
                            Identifier = javaMethod
                        }
                    },
                    Arguments = arguments.Select(visitor.Visit).ToArray()
                });
            }
            if (symbol.Name == "IsNullOrEmpty") //string.IsNullOrEmpty(s) => s.isEmpty()
            {
                return(new CallExpr
                {
                    Expression = new MemberAccessExpr
                    {
                        ObjectExpr = visitor.Visit(arguments.First()),
                        MemberExpr = new IdentifierExpr {
                            Identifier = "isEmpty"
                        }
                    },
                    Arguments = new IStmt[0]
                });
            }
            return(null);
        }
Exemple #23
0
        public IStmt Translate(ElementAccessExpressionSyntax expression, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
        {
            var symbol = GetStringSymbol(semanticModel, expression);

            if (symbol == null)
            {
                return(null);
            }
            return(new CallExpr
            {
                Expression = new MemberAccessExpr
                {
                    ObjectExpr = visitor.Visit(expression.Expression),
                    MemberExpr = new IdentifierExpr {
                        Identifier = "charAt"
                    }
                },
                Arguments = new[] { visitor.Visit(expression.ArgumentList.Arguments.First()) }
            });
        }
Exemple #24
0
        public static JavaField TranslateProp(SemanticModel semanticModel, PropertyDeclarationSyntax node, CSharpSyntaxVisitor <IStmt> statementVisitor)
        {
            var symbol = semanticModel.GetDeclaredSymbol(node);

            return(new JavaField
            {
                FieldName = node.Identifier.ToString(),
                IsStatic = symbol.IsStatic,
                Initialization = node.Initializer != null?statementVisitor.Visit(node.Initializer) : null,
                                     TypeSymbol = SymbolHelper.GetVariableSymbol(symbol),
                                     DeclaredAccessibility = symbol.DeclaredAccessibility
            });
        }
Exemple #25
0
 public static IStmt Translate <TNode>(this IEnumerable <IExtensionPoint> extensionPoints, TNode node, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
 {
     return
         (extensionPoints.OfType <IExtensionPoint <TNode> >().Select(extensionPoint => extensionPoint.Translate(node, semanticModel, visitor)).FirstOrDefault(result => result != null));
 }
        public SyntaxNode VisitInvocation(InvocationExpressionSyntax node, IMethodSymbol symbol, CSharpSyntaxVisitor <SyntaxNode> visitor)
        {
            //先将第一个范型参数转换为模型Id,忽略第二个范型参数
            ServiceCodeGenerator generator = (ServiceCodeGenerator)visitor;
            var appName       = symbol.TypeArguments[0].ContainingNamespace.ContainingNamespace.Name;
            var modelTypeName = symbol.TypeArguments[0].Name;
            var appNode       = generator.hub.DesignTree.FindApplicationNodeByName(appName);
            var modelNode     = generator.hub.DesignTree.FindModelNodeByName(appNode.Model.Id, ModelType.Entity, modelTypeName);

            var exp = SyntaxFactory.ParseExpression("appbox.Store.EntityStore.LoadEntitySetAsync");

            //第一个参数
            var modelIdArg = SyntaxFactory.Argument(
                SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(modelNode.Model.Id)));
            var args = SyntaxFactory.ArgumentList().AddArguments(modelIdArg);

            //第二个参数
            args = args.AddArguments((ArgumentSyntax)visitor.Visit(node.ArgumentList.Arguments[0]));
            //第三个参数
            var target = node.ArgumentList.Arguments[1].Expression;
            MemberAccessExpressionSyntax memberAccess = null;

            if (target is SimpleLambdaExpressionSyntax)
            {
                memberAccess = ((SimpleLambdaExpressionSyntax)target).Body as MemberAccessExpressionSyntax;
            }
            else if (target is ParenthesizedLambdaExpressionSyntax)
            {
                memberAccess = ((ParenthesizedLambdaExpressionSyntax)target).Body as MemberAccessExpressionSyntax;
            }

            if (memberAccess == null)
            {
                throw new ArgumentException("LoadEntitySetAsync参数错误");
            }
            var expSymbol = generator.SemanticModel.GetSymbolInfo(memberAccess).Symbol;
            var memberId  = generator.GetEntityMemberId(expSymbol);
            var arg3      = SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression
                                                                                   , SyntaxFactory.Literal(memberId)));

            args = args.AddArguments(arg3);

            var res = SyntaxFactory.InvocationExpression(exp, args).WithTriviaFrom(node);

            return(res);
        }
 public override TResult Accept <TResult>(CSharpSyntaxVisitor <TResult> visitor)
 {
     throw new NotImplementedException();
 }
Exemple #28
0
        public IStmt Translate(ObjectCreationExpressionSyntax node, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
        {
            var type = semanticModel.GetTypeInfo(node).Type;

            if (type == null || type.Name != "Func")
            {
                return(null);
            }
            var lambdas = node.DescendantNodes().OfType <SimpleLambdaExpressionSyntax>();

            if (!lambdas.Any())
            {
                return(null);
            }
            var lambda = lambdas.First();

            return(new BinaryExpr
            {
                Left = visitor.Visit(lambda.Parameter),
                Operation = "->",
                Right = visitor.Visit(lambda.Body)
            });
        }
        public IStmt Translate(InvocationExpressionSyntax expression, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
        {
            var symbol = GetConvertSymbol(semanticModel, expression);

            if (symbol == null)
            {
                return(null);
            }
            var arguments       = expression.ArgumentList.Arguments;
            var exactSignatures = new Dictionary <string, string>
            {
                { "ToInt32", "Integer.parseInt" },
                { "ToDouble", "Double.parseDouble" }
            };

            if (exactSignatures.ContainsKey(symbol.Name))
            {
                var dotIndex = exactSignatures[symbol.Name].IndexOf('.');
                var obj      = exactSignatures[symbol.Name].Substring(0, dotIndex);
                var method   = exactSignatures[symbol.Name].Substring(dotIndex + 1);
                return(new CallExpr
                {
                    Expression = new MemberAccessExpr
                    {
                        ObjectExpr = new IdentifierExpr {
                            Identifier = obj
                        },
                        MemberExpr = new IdentifierExpr {
                            Identifier = method
                        }
                    },
                    Arguments = new[] { visitor.Visit(arguments[0]) }
                });
            }
            return(null);
        }
Exemple #30
0
        public IStmt Translate(InvocationExpressionSyntax node, SemanticModel semanticModel, CSharpSyntaxVisitor <IStmt> visitor)
        {
            var symbol = semanticModel.GetSymbolInfo(node).Symbol;

            if (symbol == null || !SymbolHelper.IsAssignableFrom(symbol, "Func"))
            {
                return(null);
            }
            var arguments = node.ArgumentList.Arguments;

            return(new CallExpr
            {
                Expression = new MemberAccessExpr
                {
                    ObjectExpr = visitor.Visit(node.Expression),
                    MemberExpr = new IdentifierExpr {
                        Identifier = "apply"
                    }
                },
                Arguments = arguments.Select(visitor.Visit).ToArray()
            });
        }