/// <summary> /// Replaces <paramref name="methodDeclaration" />'s body with an invocation of the port's delegate. /// </summary> /// <param name="methodDeclaration">The method declaration whose body should be replaced.</param> private MethodDeclarationSyntax ReplaceBodyWithDelegateInvocation(MethodDeclarationSyntax methodDeclaration) { var fieldReference = SyntaxFactory.ParseExpression("this." + GetFieldName()); var arguments = methodDeclaration.ParameterList.Parameters.Select(parameter => { var argument = SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier)); if (parameter.Modifiers.IndexOf(SyntaxKind.RefKeyword) != -1) return argument.WithRefOrOutKeyword(SyntaxFactory.Token(SyntaxKind.RefKeyword)); if (parameter.Modifiers.IndexOf(SyntaxKind.OutKeyword) != -1) return argument.WithRefOrOutKeyword(SyntaxFactory.Token(SyntaxKind.OutKeyword)); return argument; }); var argumentList = SyntaxFactory.SeparatedList(arguments); var body = SyntaxFactory.InvocationExpression(fieldReference, SyntaxFactory.ArgumentList(argumentList)); var arrowExpression = SyntaxFactory.ArrowExpressionClause(body); methodDeclaration = methodDeclaration.WithBody(null).WithExpressionBody(arrowExpression.NormalizeWhitespace()); if (methodDeclaration.SemicolonToken.Kind() != SyntaxKind.SemicolonToken) return methodDeclaration.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)); return methodDeclaration; }
/// <summary> /// Normalizes the <paramref name="declaration" />. /// </summary> public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax declaration) { // Nothing to do here for methods without expression bodies if (declaration.ExpressionBody == null) return declaration; // Nothing to do here for methods not defined in fault effects or for methods that are no overrides of some port var methodSymbol = declaration.GetMethodSymbol(SemanticModel); if (!methodSymbol.ContainingType.IsFaultEffect(SemanticModel) || !methodSymbol.IsOverride) return declaration; var originalDeclaration = declaration; var statements = AsStatementBody(methodSymbol, declaration.ExpressionBody.Expression); declaration = declaration.WithSemicolonToken(default(SyntaxToken)).WithExpressionBody(null); return declaration.WithBody(statements).EnsureLineCount(originalDeclaration); }