コード例 #1
0
        private SyntaxNode FixMethod(
            bool keepVoid, IMethodSymbol methodSymbol, MethodDeclarationSyntax method,
            ITypeSymbol taskType, INamedTypeSymbol taskOfTType)
        {
            var newReturnType = method.ReturnType;

            if (methodSymbol.ReturnsVoid)
            {
                if (!keepVoid)
                {
                    newReturnType = taskType.GenerateTypeSyntax();
                }
            }
            else
            {
                if (!IsTaskLike(methodSymbol.ReturnType, taskType, taskOfTType))
                {
                    // If it's not already Task-like, then wrap the existing return type
                    // in Task<>.
                    newReturnType = taskOfTType.Construct(methodSymbol.ReturnType).GenerateTypeSyntax();
                }
            }

            var newModifiers = method.Modifiers.Add(s_asyncToken);
            return method.WithReturnType(newReturnType).WithModifiers(newModifiers);
        }
コード例 #2
0
        private async Task<Document> Convert2Task(Document document, MethodDeclarationSyntax methodDecl, CancellationToken cancellationToken)
        {
            var newType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task").WithAdditionalAnnotations(Simplifier.Annotation).WithTrailingTrivia(methodDecl.ReturnType.GetTrailingTrivia());
            var newMethodDecl = methodDecl.WithReturnType(newType);

            var root = await document.GetSyntaxRootAsync().ConfigureAwait(false);
            var newRoot = root.ReplaceNode(methodDecl, newMethodDecl);
            return document.WithSyntaxRoot(newRoot);
        }
コード例 #3
0
        private async Task<Document> VoidToTaskAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
        {
            // The Task object must be parsed from a string using the Syntax Factory
            var newType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task").WithAdditionalAnnotations(Simplifier.Annotation).WithTrailingTrivia(methodDeclaration.ReturnType.GetTrailingTrivia());

            var newMethodDeclaration = methodDeclaration.WithReturnType(newType);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var newRoot = oldRoot.ReplaceNode(methodDeclaration, newMethodDeclaration);
            var newDocument = document.WithSyntaxRoot(newRoot);

            // Return document with transformed tree.
            return newDocument;
        }
コード例 #4
0
        private SyntaxNode FixMethod(IMethodSymbol methodSymbol, MethodDeclarationSyntax method, ITypeSymbol taskType, ITypeSymbol taskOfTType)
        {
            var newReturnType = method.ReturnType;

            // If the return type is Task<T>, then make the new return type "T".
            // If it is Task, then make the new return type "void".
            if (methodSymbol.ReturnType.OriginalDefinition.Equals(taskType))
            {
                newReturnType = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)).WithTriviaFrom(method.ReturnType);
            }
            else if (methodSymbol.ReturnType.OriginalDefinition.Equals(taskOfTType))
            {
                newReturnType = methodSymbol.ReturnType.GetTypeArguments()[0].GenerateTypeSyntax().WithTriviaFrom(method.ReturnType);
            }

            var asyncTokenIndex = method.Modifiers.IndexOf(SyntaxKind.AsyncKeyword);
            SyntaxTokenList newModifiers;
            if (asyncTokenIndex == 0)
            {
                // Have to move the trivia on teh async token appropriately.
                var asyncLeadingTrivia = method.Modifiers[0].LeadingTrivia;

                if (method.Modifiers.Count > 1)
                {
                    // Move the trivia to the next modifier;
                    newModifiers = method.Modifiers.Replace(
                        method.Modifiers[1],
                        method.Modifiers[1].WithPrependedLeadingTrivia(asyncLeadingTrivia));
                    newModifiers = newModifiers.RemoveAt(0);
                }
                else
                {
                    // move it to the return type.
                    newModifiers = method.Modifiers.RemoveAt(0);
                    newReturnType = newReturnType.WithPrependedLeadingTrivia(asyncLeadingTrivia);
                }
            }
            else
            {
                newModifiers = method.Modifiers.RemoveAt(asyncTokenIndex);
            }

            return method.WithReturnType(newReturnType).WithModifiers(newModifiers);
        }
コード例 #5
0
        private static SyntaxNode HandleMethodDeclaration(MethodDeclarationSyntax node)
        {
            TypeSyntax type = node.ReturnType;
            if (type == null || type.IsMissing)
            {
                return null;
            }

            SyntaxTokenList modifiers = DeclarationModifiersHelper.AddModifier(node.Modifiers, ref type, SyntaxKind.PrivateKeyword);
            return node
                .WithReturnType(type)
                .WithModifiers(modifiers)
                .WithoutFormatting();
        }
		private MethodDeclarationSyntax ConvertToAsyncFunction(MethodDeclarationSyntax methodDeclaration)
		{
			return methodDeclaration.WithReturnType(
				SyntaxFactory.ParseTypeName("Task")
				.WithLeadingTrivia(methodDeclaration.ReturnType.GetLeadingTrivia())
				.WithTrailingTrivia(methodDeclaration.ReturnType.GetTrailingTrivia()));
		}
コード例 #7
0
 // set method return type to returnType
 protected internal static SyntaxNode MethodReturnType(MethodDeclarationSyntax methodDeclaration, string returnType)
 {
     TypeSyntax voidType = SyntaxFactory.ParseTypeName(returnType).WithTrailingTrivia(SyntaxFactory.Whitespace(" "));
     methodDeclaration = methodDeclaration.WithReturnType(voidType);
     return methodDeclaration;
 }
コード例 #8
0
ファイル: Compiler.cs プロジェクト: mpmedia/Excess
        private SyntaxNode rewriteEventHandler(MethodDeclarationSyntax node)
        {
            var code = (BlockSyntax)VisitBlock(node.Body);
            var args = node.ParameterList.ReplaceNodes(node.ParameterList.Parameters, (oldParam, newParam) =>
            {
                if (oldParam.Identifier.IsMissing)
                    return newParam.WithType(Void).
                                    WithIdentifier(SyntaxFactory.Identifier(newParam.Type.ToString()));

                return newParam;
            });

            var result = node.WithReturnType(Void).
                              WithParameterList(args).
                              WithModifiers(Private).
                              WithBody(code);

            return ctx_.AddLinker(result, (ctx, linkNode, newNode, model) =>
            {
                MethodDeclarationSyntax mthd   = (MethodDeclarationSyntax)linkNode;
                MethodDeclarationSyntax output = (MethodDeclarationSyntax)newNode;

                ParameterListSyntax methdArgs = mthd.ParameterList;

                string methodName = "on_" + mthd.Identifier.ToString();

                ISymbol     self = model.GetDeclaredSymbol(mthd);
                ITypeSymbol type = (ITypeSymbol)self.ContainingSymbol;

                string evName   = mthd.Identifier.ToString();
                string typeName = type.Name;
                bool   found    = false;
                while (type != null && !found)
                {
                    foreach (var ev in type.GetMembers().OfType<IEventSymbol>())
                    {
                        if (matchEventName(ev.Name, evName))
                        {
                            //arguments
                            foreach (var syntax in ev.Type.DeclaringSyntaxReferences)
                            {
                                var refNode = (DelegateDeclarationSyntax)syntax.GetSyntax();

                                int  pCount = methdArgs.Parameters.Count;
                                int  idx    = 0;
                                bool match  = true;
                                args = refNode.ParameterList.ReplaceNodes(refNode.ParameterList.Parameters, (oldArg, newArg) =>
                                {
                                    if (match)
                                    {
                                        if (idx >= pCount && match)
                                            return newArg;

                                        ParameterSyntax arg = methdArgs.Parameters[idx++];
                                        string argName = arg.Identifier.ToString();
                                        if (argName == oldArg.Identifier.ToString())
                                        {
                                            //coincident parameters, fix missing type or return same
                                            if (arg.Identifier.IsMissing || arg.Type.ToString() == "void")
                                                return newArg.WithIdentifier(arg.Identifier);

                                            return arg;
                                        }
                                        else
                                        {
                                            match = false;
                                            if (!refNode.ParameterList.Parameters.Any(p => p.Identifier.ToString().Equals(arg.Identifier.ToString())))
                                            {
                                                //name change?
                                                if (oldArg.Identifier.IsMissing)
                                                    return newArg.WithIdentifier(SyntaxFactory.Identifier(arg.Type.ToString()));

                                                return arg;
                                            }
                                        }
                                    }

                                    return newArg;
                                });
                            }

                            //register event initialization
                            ctx.AddTypeInfo(typeName, "initializer", EventInitializer(ev.Name, methodName));
                            found = true;
                            break;
                        }
                    }

                    type = type.BaseType;
                }

                if (!found)
                {
                    //td: error, no such event
                }

                return output.WithIdentifier(SyntaxFactory.Identifier(methodName)).
                              WithParameterList(args);
            });
        }
 private MethodDeclarationSyntax ConvertToAsyncFunction(MethodDeclarationSyntax methodDeclaration)
 {
     return methodDeclaration.WithReturnType(
         SyntaxFactory.ParseTypeName("Task")
             .WithTriviaFrom(methodDeclaration));
 }