OperationTreeVerifier.cs 42.3 KB
Newer Older
1 2 3 4 5 6
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
7
using System.Linq;
8
using System.Text;
9
using System.Text.RegularExpressions;
10 11
using Microsoft.CodeAnalysis.Semantics;
using Microsoft.CodeAnalysis.Test.Extensions;
M
Manish Vasani 已提交
12
using Microsoft.CodeAnalysis.Text;
13
using Roslyn.Test.Utilities;
14
using Roslyn.Utilities;
15 16 17 18 19 20
using Xunit;

namespace Microsoft.CodeAnalysis.Test.Utilities
{
    public sealed class OperationTreeVerifier : OperationWalker
    {
H
Heejae Chang 已提交
21
        private readonly Compilation _compilation;
22 23 24 25 26 27 28
        private readonly IOperation _root;
        private readonly StringBuilder _builder;

        private const string indent = "  ";
        private string _currentIndent;
        private bool _pendingIndent;

H
Heejae Chang 已提交
29
        public OperationTreeVerifier(Compilation compilation, IOperation root, int initialIndent)
30
        {
H
Heejae Chang 已提交
31
            _compilation = compilation;
32 33 34 35 36 37 38
            _root = root;
            _builder = new StringBuilder();

            _currentIndent = new string(' ', initialIndent);
            _pendingIndent = true;
        }

H
Heejae Chang 已提交
39
        public static void Verify(Compilation compilation, IOperation operation, string expectedOperationTree, int initialIndent = 0)
40
        {
H
Heejae Chang 已提交
41
            var actual = GetOperationTree(compilation, operation, initialIndent);
42 43 44
            Assert.Equal(expectedOperationTree, actual);
        }

H
Heejae Chang 已提交
45
        public static string GetOperationTree(Compilation compilation, IOperation operation, int initialIndent = 0)
46
        {
H
Heejae Chang 已提交
47
            var walker = new OperationTreeVerifier(compilation, operation, initialIndent);
48 49 50 51
            walker.Visit(operation);
            return walker._builder.ToString();
        }

52 53 54 55 56 57 58 59 60
        public static void Verify(string expectedOperationTree, string actualOperationTree)
        {
            char[] newLineChars = Environment.NewLine.ToCharArray();
            string actual = actualOperationTree.Trim(newLineChars);
            expectedOperationTree = expectedOperationTree.Trim(newLineChars);
            expectedOperationTree = Regex.Replace(expectedOperationTree, "([^\r])\n", "$1" + Environment.NewLine);
            AssertEx.AreEqual(expectedOperationTree, actual);
        }

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        #region Logging helpers

        private void LogCommonPropertiesAndNewLine(IOperation operation)
        {
            LogString(" (");

            // Kind
            LogString($"{nameof(OperationKind)}.{operation.Kind}");

            // Type
            if (ShouldLogType(operation))
            {
                LogString(", ");
                LogType(operation.Type);
            }

            // ConstantValue
            if (operation.ConstantValue.HasValue)
            {
                LogString(", ");
                LogConstant(operation.ConstantValue);
            }

            // IsInvalid
H
Heejae Chang 已提交
85
            if (operation.IsInvalid(_compilation))
86 87 88 89 90
            {
                LogString(", IsInvalid");
            }

            LogString(")");
M
Manish Vasani 已提交
91 92 93 94

            // Syntax
            LogString($" (Syntax: {GetSnippetFromSyntax(operation.Syntax)})");

95 96 97
            LogNewLine();
        }

M
Manish Vasani 已提交
98 99 100 101 102 103 104
        private static string GetSnippetFromSyntax(SyntaxNode syntax)
        {
            if (syntax == null)
            {
                return "null";
            }

M
Manish Vasani 已提交
105
            var text = syntax.ToString();
106 107
            var lines = text.Split(new[] { Environment.NewLine, "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).Select(l => l.Trim()).ToArray();
            if (lines.Length <= 1 && text.Length < 25)
M
Manish Vasani 已提交
108
            {
109
                return $"'{text}'";
M
Manish Vasani 已提交
110 111
            }

112
            const int maxTokenLength = 11;
M
Manish Vasani 已提交
113 114
            var firstLine = lines[0];
            var lastLine = lines[lines.Length - 1];
115 116 117
            var prefix = firstLine.Length <= maxTokenLength ? firstLine : firstLine.Substring(0, maxTokenLength);
            var suffix = lastLine.Length <= maxTokenLength ? lastLine : lastLine.Substring(lastLine.Length - maxTokenLength, maxTokenLength);
            return $"'{prefix} ... {suffix}'";
M
Manish Vasani 已提交
118 119
        }

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        private static bool ShouldLogType(IOperation operation)
        {
            var operationKind = (int)operation.Kind;

            // Expressions
            if (operationKind >= 0x100 && operationKind < 0x400)
            {
                return true;
            }

            return false;
        }

        private void LogString(string str)
        {
            if (_pendingIndent)
            {
                str = _currentIndent + str;
                _pendingIndent = false;
            }

            _builder.Append(str);
        }

        private void LogNewLine()
        {
            LogString(Environment.NewLine);
            _pendingIndent = true;
        }

        private void Indent()
        {
            _currentIndent += indent;
        }

        private void Unindent()
        {
            _currentIndent = _currentIndent.Substring(indent.Length);
        }

        private void LogConstant(Optional<object> constant, string header = "Constant")
        {
            if (constant.HasValue)
            {
                LogConstant(constant.Value, header);
            }
        }

        private void LogConstant(object constant, string header = "Constant")
        {
            var valueStr = constant != null ? constant.ToString() : "null";
171
            if (constant is string)
M
Manish Vasani 已提交
172
            {
M
Manish Vasani 已提交
173
                valueStr = @"""" + valueStr + @"""";
M
Manish Vasani 已提交
174
            }
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
            LogString($"{header}: {valueStr}");
        }

        private void LogSymbol(ISymbol symbol, string header, bool logDisplayString = true)
        {
            if (!string.IsNullOrEmpty(header))
            {
                LogString($"{header}: ");
            }

            var symbolStr = symbol != null ? (logDisplayString ? symbol.ToTestDisplayString() : symbol.Name) : "null";
            LogString($"{symbolStr}");
        }

        private void LogType(ITypeSymbol type)
        {
            var typeStr = type != null ? type.ToTestDisplayString() : "null";
            LogString($"Type: {typeStr}");
        }

        #endregion

        #region Visit methods

        public override void Visit(IOperation operation)
        {
            if (operation == null)
            {
204 205
                LogString("null");
                LogNewLine();
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                return;
            }

            if (operation != _root)
            {
                Indent();
            }

            base.Visit(operation);

            if (operation != _root)
            {
                Unindent();
            }
        }

        private void Visit(IOperation operation, string header)
        {
            Debug.Assert(!string.IsNullOrEmpty(header));

            Indent();
            LogString($"{header}: ");
            Visit(operation);
            Unindent();
        }

232
        private void VisitArray<T>(IEnumerable<T> list, string header, bool logElementCount)
233 234 235 236
            where T : IOperation
        {
            Debug.Assert(!string.IsNullOrEmpty(header));

237 238
            Indent();
            if (!list.IsEmpty())
239
            {
240 241 242 243 244 245 246 247 248 249 250
                var elementCount = logElementCount ? $"({list.Count()})" : string.Empty;
                LogString($"{header}{elementCount}:");
                LogNewLine();
                Indent();
                VisitArray(list);
                Unindent();
            }
            else
            {
                LogString($"{header}(0)");
                LogNewLine();
251 252
            }

253 254 255 256 257 258 259 260 261 262
            Unindent();
        }

        private void VisitInstanceExpression(IOperation instance)
        {
            Visit(instance, header: "Instance Receiver");
        }

        internal override void VisitNoneOperation(IOperation operation)
        {
263 264
            LogString("IOperation: ");
            LogCommonPropertiesAndNewLine(operation);
265

266
            if (operation is IOperationWithChildren operationWithChildren)
267
            {
268 269 270 271 272
                var children = operationWithChildren.Children.WhereNotNull().ToImmutableArray();
                if (children.Length > 0)
                {
                    VisitArray(children, "Children", logElementCount: true);
                }
273
            }
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        }

        public override void VisitBlockStatement(IBlockStatement operation)
        {
            LogString(nameof(IBlockStatement));

            var statementsStr = $"{operation.Statements.Length} statements";
            var localStr = !operation.Locals.IsEmpty ? $", {operation.Locals.Length} locals" : string.Empty;
            LogString($" ({statementsStr}{localStr})");
            LogCommonPropertiesAndNewLine(operation);

            if (operation.Statements.IsEmpty)
            {
                return;
            }

            LogLocals(operation.Locals);
            base.VisitBlockStatement(operation);
        }

        public override void VisitVariableDeclarationStatement(IVariableDeclarationStatement operation)
        {
296
            var variablesCountStr = $"{operation.Declarations.Length} declarations";
297 298 299 300 301 302 303 304
            LogString($"{nameof(IVariableDeclarationStatement)} ({variablesCountStr})");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitVariableDeclarationStatement(operation);
        }

        public override void VisitVariableDeclaration(IVariableDeclaration operation)
        {
305 306
            var symbolsCountStr = $"{operation.Variables.Length} variables";
            LogString($"{nameof(IVariableDeclaration)} ({symbolsCountStr})");
307 308
            LogCommonPropertiesAndNewLine(operation);

309
            LogLocals(operation.Variables, header: "Variables");
310

311
            Visit(operation.Initializer, "Initializer");
312 313 314 315 316 317 318 319 320
        }

        public override void VisitSwitchStatement(ISwitchStatement operation)
        {
            var caseCountStr = $"{operation.Cases.Length} cases";
            LogString($"{nameof(ISwitchStatement)} ({caseCountStr})");
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Value, header: "Switch expression");
321
            VisitArray(operation.Cases, "Sections", logElementCount: false);
322 323 324 325 326 327 328 329 330 331
        }

        public override void VisitSwitchCase(ISwitchCase operation)
        {
            var caseClauseCountStr = $"{operation.Clauses.Length} case clauses";
            var statementCountStr = $"{operation.Body.Length} statements";
            LogString($"{nameof(ISwitchCase)} ({caseClauseCountStr}, {statementCountStr})");
            LogCommonPropertiesAndNewLine(operation);

            Indent();
332 333
            VisitArray(operation.Clauses, "Clauses", logElementCount: false);
            VisitArray(operation.Body, "Body", logElementCount: false);
334 335 336 337 338 339 340 341 342 343 344
            Unindent();
        }

        public override void VisitWhileUntilLoopStatement(IWhileUntilLoopStatement operation)
        {
            LogString(nameof(IWhileUntilLoopStatement));

            LogString($" (IsTopTest: {operation.IsTopTest}, IsWhile: {operation.IsWhile})");
            LogLoopStatementHeader(operation);

            Visit(operation.Condition, "Condition");
M
Manish Vasani 已提交
345
            Visit(operation.Body, "Body");
346 347 348 349 350 351 352 353 354
        }

        public override void VisitForLoopStatement(IForLoopStatement operation)
        {
            LogString(nameof(IForLoopStatement));
            LogLoopStatementHeader(operation);

            Visit(operation.Condition, "Condition");
            LogLocals(operation.Locals);
355 356
            VisitArray(operation.Before, "Before", logElementCount: false);
            VisitArray(operation.AtLoopBottom, "AtLoopBottom", logElementCount: false);
M
Manish Vasani 已提交
357
            Visit(operation.Body, "Body");
358 359
        }

360
        private void LogLocals(IEnumerable<ILocalSymbol> locals, string header = "Locals")
361
        {
362 363 364 365 366
            if (!locals.Any())
            {
                return;
            }

367 368
            Indent();

369
            LogString($"{header}: ");
370 371 372 373 374 375 376 377 378 379
            Indent();

            int localIndex = 1;
            foreach (var local in locals)
            {
                LogSymbol(local, header: $"Local_{localIndex++}");
                LogNewLine();
            }

            Unindent();
M
Manish Vasani 已提交
380
            Unindent();
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        }

        private void LogLoopStatementHeader(ILoopStatement operation)
        {
            var kindStr = $"{nameof(LoopKind)}.{operation.LoopKind}";
            LogString($" ({kindStr})");
            LogCommonPropertiesAndNewLine(operation);
        }

        public override void VisitForEachLoopStatement(IForEachLoopStatement operation)
        {
            LogString(nameof(IForEachLoopStatement));
            LogSymbol(operation.IterationVariable, " (Iteration variable");
            LogString(")");

            LogLoopStatementHeader(operation);
            Visit(operation.Collection, "Collection");
M
Manish Vasani 已提交
398
            Visit(operation.Body, "Body");
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
        }

        public override void VisitLabelStatement(ILabelStatement operation)
        {
            LogString(nameof(ILabelStatement));

            // TODO: Put a better workaround to skip compiler generated labels.
            if (!operation.Label.IsImplicitlyDeclared)
            {
                LogString($" (Label: {operation.Label.Name})");
            }

            LogCommonPropertiesAndNewLine(operation);

            base.VisitLabelStatement(operation);
        }

        public override void VisitBranchStatement(IBranchStatement operation)
        {
            LogString(nameof(IBranchStatement));
            var kindStr = $"{nameof(BranchKind)}.{operation.BranchKind}";
            var labelStr = !operation.Target.IsImplicitlyDeclared ? $", Label: {operation.Target.Name}" : string.Empty;
            LogString($" ({kindStr}{labelStr})");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitBranchStatement(operation);
        }

        public override void VisitYieldBreakStatement(IReturnStatement operation)
        {
G
Gen Lu 已提交
429
            LogString(nameof(IReturnStatement));
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
            LogCommonPropertiesAndNewLine(operation);

            base.VisitYieldBreakStatement(operation);
        }

        public override void VisitEmptyStatement(IEmptyStatement operation)
        {
            LogString(nameof(IEmptyStatement));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitEmptyStatement(operation);
        }

        public override void VisitThrowStatement(IThrowStatement operation)
        {
            LogString(nameof(IThrowStatement));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitThrowStatement(operation);
        }

        public override void VisitReturnStatement(IReturnStatement operation)
        {
            LogString(nameof(IReturnStatement));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitReturnStatement(operation);
        }

        public override void VisitLockStatement(ILockStatement operation)
        {
            LogString(nameof(ILockStatement));
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
464 465
            Visit(operation.LockedObject, "LockedObject");
            Visit(operation.Body, "Body");
466 467 468 469 470 471 472
        }

        public override void VisitTryStatement(ITryStatement operation)
        {
            LogString(nameof(ITryStatement));
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
473 474 475
            Visit(operation.Body, "Body");
            VisitArray(operation.Catches, "Catch clauses", logElementCount: true);
            Visit(operation.FinallyHandler, "Finally");
476 477
        }

478
        public override void VisitCatchClause(ICatchClause operation)
479 480
        {
            LogString(nameof(ICatchClause));
M
Manish Vasani 已提交
481
            LogString($" (Exception type: {operation.CaughtType?.ToTestDisplayString()}, Exception local: {operation.ExceptionLocal?.ToTestDisplayString()})");
482 483
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
484 485
            Visit(operation.Filter, "Filter");
            Visit(operation.Handler, "Handler");
486 487 488 489 490 491 492
        }

        public override void VisitUsingStatement(IUsingStatement operation)
        {
            LogString(nameof(IUsingStatement));
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
493 494 495
            Visit(operation.Declaration, "Declaration");
            Visit(operation.Value, "Value");
            Visit(operation.Body, "Body");
496 497 498 499 500 501 502
        }

        public override void VisitFixedStatement(IFixedStatement operation)
        {
            LogString(nameof(IFixedStatement));
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
503 504
            Visit(operation.Variables, "Declaration");
            Visit(operation.Body, "Body");
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        }

        public override void VisitExpressionStatement(IExpressionStatement operation)
        {
            LogString(nameof(IExpressionStatement));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitExpressionStatement(operation);
        }

        public override void VisitWithStatement(IWithStatement operation)
        {
            LogString(nameof(IWithStatement));
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
520 521
            Visit(operation.Value, "Value");
            Visit(operation.Body, "Body");
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
        }

        public override void VisitStopStatement(IStopStatement operation)
        {
            LogString(nameof(IStopStatement));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitStopStatement(operation);
        }

        public override void VisitEndStatement(IEndStatement operation)
        {
            LogString(nameof(IEndStatement));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitEndStatement(operation);
        }

        public override void VisitInvocationExpression(IInvocationExpression operation)
        {
            LogString(nameof(IInvocationExpression));

            var isVirtualStr = operation.IsVirtual ? "virtual " : string.Empty;
            var spacing = !operation.IsVirtual && operation.Instance != null ? " " : string.Empty;
546
            LogString($" ({isVirtualStr}{spacing}");
547 548 549 550 551 552 553 554
            LogSymbol(operation.TargetMethod, header: string.Empty);
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);

            VisitInstanceExpression(operation.Instance);
            VisitArguments(operation);
        }

M
Manish Vasani 已提交
555
        private void VisitArguments(IHasArgumentsExpression operation)
556
        {
557
            VisitArray(operation.ArgumentsInEvaluationOrder, "Arguments", logElementCount: true);
558 559 560 561 562
        }

        public override void VisitArgument(IArgument operation)
        {
            LogString($"{nameof(IArgument)} (");
563
            LogString($"{nameof(ArgumentKind)}.{operation.ArgumentKind}, ");
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
            LogSymbol(operation.Parameter, header: "Matching Parameter", logDisplayString: false);
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Value);
            Visit(operation.InConversion, "InConversion");
            Visit(operation.OutConversion, "OutConversion");
        }

        public override void VisitOmittedArgumentExpression(IOmittedArgumentExpression operation)
        {
            LogString(nameof(IOmittedArgumentExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitOmittedArgumentExpression(operation);
        }

        public override void VisitArrayElementReferenceExpression(IArrayElementReferenceExpression operation)
        {
            LogString(nameof(IArrayElementReferenceExpression));
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
586 587
            Visit(operation.ArrayReference, "Array reference");
            VisitArray(operation.Indices, "Indices", logElementCount: true);
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
        }

        public override void VisitPointerIndirectionReferenceExpression(IPointerIndirectionReferenceExpression operation)
        {
            LogString(nameof(IPointerIndirectionReferenceExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitPointerIndirectionReferenceExpression(operation);
        }

        public override void VisitLocalReferenceExpression(ILocalReferenceExpression operation)
        {
            LogString(nameof(ILocalReferenceExpression));
            LogString($": {operation.Local.Name}");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitLocalReferenceExpression(operation);
        }

        public override void VisitParameterReferenceExpression(IParameterReferenceExpression operation)
        {
            LogString(nameof(IParameterReferenceExpression));
            LogString($": {operation.Parameter.Name}");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitParameterReferenceExpression(operation);
        }

        public override void VisitSyntheticLocalReferenceExpression(ISyntheticLocalReferenceExpression operation)
        {
            LogString(nameof(ISyntheticLocalReferenceExpression));
            var kindStr = $"{nameof(SynthesizedLocalKind)}.{operation.SyntheticLocalKind}";
            LogString($" ({kindStr})");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitSyntheticLocalReferenceExpression(operation);
        }

        public override void VisitInstanceReferenceExpression(IInstanceReferenceExpression operation)
        {
            LogString(nameof(IInstanceReferenceExpression));
            var kindStr = $"{nameof(InstanceReferenceKind)}.{operation.InstanceReferenceKind}";
            LogString($" ({kindStr})");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitInstanceReferenceExpression(operation);
        }

        private void VisitMemberReferenceExpressionCommon(IMemberReferenceExpression operation)
        {
            if (operation.Instance == null)
            {
                LogString(" (Static)");
            }

            LogCommonPropertiesAndNewLine(operation);
            VisitInstanceExpression(operation.Instance);
        }

        public override void VisitFieldReferenceExpression(IFieldReferenceExpression operation)
        {
            LogString(nameof(IFieldReferenceExpression));
            LogString($": {operation.Field.ToTestDisplayString()}");

            VisitMemberReferenceExpressionCommon(operation);
        }

        public override void VisitMethodBindingExpression(IMethodBindingExpression operation)
        {
            LogString(nameof(IMethodBindingExpression));
            LogString($": {operation.Method.ToTestDisplayString()}");

            if (operation.IsVirtual)
            {
M
Manish Vasani 已提交
662
                LogString(" (IsVirtual)");
663 664 665 666 667 668 669 670 671 672 673
            }

            VisitMemberReferenceExpressionCommon(operation);
        }

        public override void VisitPropertyReferenceExpression(IPropertyReferenceExpression operation)
        {
            LogString(nameof(IPropertyReferenceExpression));
            LogString($": {operation.Property.ToTestDisplayString()}");

            VisitMemberReferenceExpressionCommon(operation);
H
Heejae Chang 已提交
674

675 676 677 678
            if (operation.ArgumentsInEvaluationOrder.Length > 0)
            {
                VisitArguments(operation);
            }
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
        }

        public override void VisitEventReferenceExpression(IEventReferenceExpression operation)
        {
            LogString(nameof(IEventReferenceExpression));
            LogString($": {operation.Event.ToTestDisplayString()}");

            VisitMemberReferenceExpressionCommon(operation);
        }

        public override void VisitEventAssignmentExpression(IEventAssignmentExpression operation)
        {
            var kindStr = operation.Adds ? "EventAdd" : "EventRemove";
            LogString($"{nameof(IEventAssignmentExpression)} ({kindStr})");
            LogSymbol(operation.Event, header: " (Event: ");
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.EventInstance, header: "Event Instance");
            Visit(operation.HandlerValue, header: "Handler");
        }

        public override void VisitConditionalAccessExpression(IConditionalAccessExpression operation)
        {
            LogString(nameof(IConditionalAccessExpression));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.ConditionalInstance, header: "Left");
            Visit(operation.ConditionalValue, header: "Right");
        }

        public override void VisitConditionalAccessInstanceExpression(IConditionalAccessInstanceExpression operation)
        {
            LogString(nameof(IConditionalAccessInstanceExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitConditionalAccessInstanceExpression(operation);
        }

        public override void VisitPlaceholderExpression(IPlaceholderExpression operation)
        {
            LogString(nameof(IPlaceholderExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitPlaceholderExpression(operation);
        }

        public override void VisitUnaryOperatorExpression(IUnaryOperatorExpression operation)
        {
            LogString(nameof(IUnaryOperatorExpression));

            var kindStr = $"{nameof(UnaryOperationKind)}.{operation.UnaryOperationKind}";
            LogString($" ({kindStr})");
            LogHasOperatorMethodExpressionCommon(operation);
            LogCommonPropertiesAndNewLine(operation);

            base.VisitUnaryOperatorExpression(operation);
        }

        public override void VisitBinaryOperatorExpression(IBinaryOperatorExpression operation)
        {
            LogString(nameof(IBinaryOperatorExpression));

            var kindStr = $"{nameof(BinaryOperationKind)}.{operation.BinaryOperationKind}";
            LogString($" ({kindStr})");
            LogHasOperatorMethodExpressionCommon(operation);
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.LeftOperand, "Left");
            Visit(operation.RightOperand, "Right");
        }

        private void LogHasOperatorMethodExpressionCommon(IHasOperatorMethodExpression operation)
        {
            Assert.Equal(operation.UsesOperatorMethod, operation.OperatorMethod != null);

            if (!operation.UsesOperatorMethod)
            {
                return;
            }

            LogSymbol(operation.OperatorMethod, header: " (OperatorMethod");
            LogString(")");
        }

        public override void VisitConversionExpression(IConversionExpression operation)
        {
            LogString(nameof(IConversionExpression));

            var kindStr = $"{nameof(ConversionKind)}.{operation.ConversionKind}";
            var isExplicitStr = operation.IsExplicit ? "Explicit" : "Implicit";
            LogString($" ({kindStr}, {isExplicitStr})");

            LogHasOperatorMethodExpressionCommon(operation);
            LogCommonPropertiesAndNewLine(operation);

            base.VisitConversionExpression(operation);
        }

        public override void VisitConditionalChoiceExpression(IConditionalChoiceExpression operation)
        {
            LogString(nameof(IConditionalChoiceExpression));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Condition, "Condition");
            Visit(operation.IfTrueValue, "IfTrue");
            Visit(operation.IfFalseValue, "IfFalse");
        }

        public override void VisitNullCoalescingExpression(INullCoalescingExpression operation)
        {
            LogString(nameof(INullCoalescingExpression));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.PrimaryOperand, "Left");
            Visit(operation.SecondaryOperand, "Right");
        }

        public override void VisitIsTypeExpression(IIsTypeExpression operation)
        {
            LogString(nameof(IIsTypeExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitIsTypeExpression(operation);

            Indent();
            LogType(operation.Type);
            Unindent();
        }

        private void LogTypeOperationExpressionCommon(ITypeOperationExpression operation)
        {
            LogString(" (");
            LogType(operation.TypeOperand);
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);
        }

        public override void VisitSizeOfExpression(ISizeOfExpression operation)
        {
            LogString(nameof(ISizeOfExpression));
            LogTypeOperationExpressionCommon(operation);

            base.VisitSizeOfExpression(operation);
        }

        public override void VisitTypeOfExpression(ITypeOfExpression operation)
        {
            LogString(nameof(ITypeOfExpression));
            LogTypeOperationExpressionCommon(operation);

            base.VisitTypeOfExpression(operation);
        }

        public override void VisitLambdaExpression(ILambdaExpression operation)
        {
            LogString(nameof(ILambdaExpression));

            LogSymbol(operation.Signature, header: " (Signature");
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitLambdaExpression(operation);
        }

        public override void VisitLiteralExpression(ILiteralExpression operation)
        {
            LogString(nameof(ILiteralExpression));

848 849
            object value;
            if (operation.ConstantValue.HasValue && ((value = operation.ConstantValue.Value) == null ? "null" : value.ToString()) == operation.Text)
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
            {
                LogString($" (Text: {operation.Text})");
            }

            LogCommonPropertiesAndNewLine(operation);

            base.VisitLiteralExpression(operation);
        }

        public override void VisitAwaitExpression(IAwaitExpression operation)
        {
            LogString(nameof(IAwaitExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitAwaitExpression(operation);
        }

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
        public override void VisitNameOfExpression(INameOfExpression operation)
        {
            LogString(nameof(INameOfExpression));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Argument);
        }

        public override void VisitThrowExpression(IThrowExpression operation)
        {
            LogString(nameof(IThrowExpression));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Expression);
        }

883 884 885 886 887 888 889 890 891 892 893 894 895 896
        public override void VisitAddressOfExpression(IAddressOfExpression operation)
        {
            LogString(nameof(IAddressOfExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitAddressOfExpression(operation);
        }

        public override void VisitObjectCreationExpression(IObjectCreationExpression operation)
        {
            LogString(nameof(IObjectCreationExpression));
            LogString($" (Constructor: {operation.Constructor.ToTestDisplayString()})");
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
897
            VisitArguments(operation);
898
            Visit(operation.Initializer, "Initializer");
899 900
        }

901 902 903 904 905 906
        public override void VisitAnonymousObjectCreationExpression(IAnonymousObjectCreationExpression operation)
        {
            LogString(nameof(IAnonymousObjectCreationExpression));
            LogCommonPropertiesAndNewLine(operation);

            VisitArray(operation.Initializers, "Initializers", logElementCount: true);
907 908
        }

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
        public override void VisitObjectOrCollectionInitializerExpression(IObjectOrCollectionInitializerExpression operation)
        {
            LogString(nameof(IObjectOrCollectionInitializerExpression));
            LogCommonPropertiesAndNewLine(operation);

            VisitArray(operation.Initializers, "Initializers", logElementCount: true);
        }

        public override void VisitMemberInitializerExpression(IMemberInitializerExpression operation)
        {
            LogString(nameof(IMemberInitializerExpression));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.InitializedMember, "InitializedMember");
            Visit(operation.Initializer, "Initializer");
        }

        public override void VisitCollectionElementInitializerExpression(ICollectionElementInitializerExpression operation)
        {
            LogString(nameof(ICollectionElementInitializerExpression));
            if (operation.AddMethod != null)
            {
                LogString($" (AddMethod: {operation.AddMethod.ToTestDisplayString()})");
            }
            LogString($" (IsDynamic: {operation.IsDynamic})");
            LogCommonPropertiesAndNewLine(operation);

            VisitArray(operation.Arguments, "Arguments", logElementCount: true);
        }

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
        public override void VisitFieldInitializer(IFieldInitializer operation)
        {
            LogString(nameof(IFieldInitializer));

            if (operation.InitializedFields.Length <= 1)
            {
                if (operation.InitializedFields.Length == 1)
                {
                    LogSymbol(operation.InitializedFields[0], header: " (Field");
                    LogString(")");
                }

                LogCommonPropertiesAndNewLine(operation);
            }
            else
            {
                LogString($" ({operation.InitializedFields.Length} initialized fields)");
                LogCommonPropertiesAndNewLine(operation);

                Indent();

                int index = 1;
                foreach (var local in operation.InitializedFields)
                {
                    LogSymbol(local, header: $"Field_{index++}");
                    LogNewLine();
                }

                Unindent();
            }

            base.VisitFieldInitializer(operation);
        }

        public override void VisitPropertyInitializer(IPropertyInitializer operation)
        {
            LogString(nameof(IPropertyInitializer));
            LogSymbol(operation.InitializedProperty, header: " (Property");
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitPropertyInitializer(operation);
        }

        public override void VisitParameterInitializer(IParameterInitializer operation)
        {
            LogString(nameof(IParameterInitializer));
            LogSymbol(operation.Parameter, header: " (Parameter");
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);

            base.VisitParameterInitializer(operation);
        }

        public override void VisitArrayCreationExpression(IArrayCreationExpression operation)
        {
            LogString(nameof(IArrayCreationExpression));
M
Manish Vasani 已提交
996
            LogString($" (Element Type: {operation.ElementType?.ToTestDisplayString()})");
997 998
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
999 1000
            VisitArray(operation.DimensionSizes, "Dimension Sizes", logElementCount: true);
            Visit(operation.Initializer, "Initializer");
1001 1002 1003 1004 1005
        }

        public override void VisitArrayInitializer(IArrayInitializer operation)
        {
            LogString(nameof(IArrayInitializer));
1006
            LogString($" ({operation.ElementValues.Length} elements)");
1007 1008
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
1009
            VisitArray(operation.ElementValues, "Element Values", logElementCount: true);
1010 1011
        }

1012
        public override void VisitSimpleAssignmentExpression(ISimpleAssignmentExpression operation)
1013
        {
1014
            LogString(nameof(ISimpleAssignmentExpression));
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Target, "Left");
            Visit(operation.Value, "Right");
        }

        public override void VisitCompoundAssignmentExpression(ICompoundAssignmentExpression operation)
        {
            LogString(nameof(ICompoundAssignmentExpression));

            var kindStr = $"{nameof(BinaryOperationKind)}.{operation.BinaryOperationKind}";
            LogString($" ({kindStr})");
            LogHasOperatorMethodExpressionCommon(operation);
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Target, "Left");
            Visit(operation.Value, "Right");
        }

        public override void VisitIncrementExpression(IIncrementExpression operation)
        {
            LogString(nameof(IIncrementExpression));

            var unaryKindStr = $"{nameof(UnaryOperandKind)}.{operation.IncrementOperationKind}";
1039
            LogString($" ({unaryKindStr})");
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
            LogHasOperatorMethodExpressionCommon(operation);
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Target, "Left");
        }

        public override void VisitParenthesizedExpression(IParenthesizedExpression operation)
        {
            LogString(nameof(IParenthesizedExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitParenthesizedExpression(operation);
        }

        public override void VisitLateBoundMemberReferenceExpression(ILateBoundMemberReferenceExpression operation)
        {
            LogString(nameof(ILateBoundMemberReferenceExpression));
            LogString($" (Member name: {operation.MemberName})");
            LogCommonPropertiesAndNewLine(operation);

            VisitInstanceExpression(operation.Instance);
        }

        public override void VisitDefaultValueExpression(IDefaultValueExpression operation)
        {
            LogString(nameof(IDefaultValueExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitDefaultValueExpression(operation);
        }

        public override void VisitTypeParameterObjectCreationExpression(ITypeParameterObjectCreationExpression operation)
        {
            LogString(nameof(ITypeParameterObjectCreationExpression));
            LogCommonPropertiesAndNewLine(operation);

            base.VisitTypeParameterObjectCreationExpression(operation);
        }

        public override void VisitInvalidStatement(IInvalidStatement operation)
        {
            LogString(nameof(IInvalidStatement));
            LogCommonPropertiesAndNewLine(operation);

1084
            VisitArray(operation.Children, "Children", logElementCount: true);
1085 1086 1087 1088 1089 1090 1091
        }

        public override void VisitInvalidExpression(IInvalidExpression operation)
        {
            LogString(nameof(IInvalidExpression));
            LogCommonPropertiesAndNewLine(operation);

1092
            VisitArray(operation.Children, "Children", logElementCount: true);
1093 1094 1095 1096 1097 1098 1099 1100
        }

        public override void VisitIfStatement(IIfStatement operation)
        {
            LogString(nameof(IIfStatement));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Condition, "Condition");
M
Manish Vasani 已提交
1101 1102
            Visit(operation.IfTrueStatement, "IfTrue");
            Visit(operation.IfFalseStatement, "IfFalse");
1103 1104
        }

1105 1106 1107 1108 1109 1110 1111 1112
        public override void VisitLocalFunctionStatement(ILocalFunctionStatement operation)
        {
            LogString(nameof(ILocalFunctionStatement));

            LogSymbol(operation.LocalFunctionSymbol, header: " (Local Function");
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);

M
Manish Vasani 已提交
1113
            Visit(operation.Body);
1114 1115
        }

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
        private void LogCaseClauseCommon(ICaseClause operation)
        {
            var kindStr = $"{nameof(CaseKind)}.{operation.CaseKind}";
            LogString($" ({kindStr})");
            LogCommonPropertiesAndNewLine(operation);
        }

        public override void VisitSingleValueCaseClause(ISingleValueCaseClause operation)
        {
            LogString(nameof(ISingleValueCaseClause));
            var kindStr = $"{nameof(BinaryOperationKind)}.{operation.Equality}";
            LogString($" (Equality operator kind: {kindStr})");
            LogCaseClauseCommon(operation);

            base.VisitSingleValueCaseClause(operation);
        }

        public override void VisitRelationalCaseClause(IRelationalCaseClause operation)
        {
            LogString(nameof(IRelationalCaseClause));
            var kindStr = $"{nameof(BinaryOperationKind)}.{operation.Relation}";
            LogString($" (Relational operator kind: {kindStr})");
            LogCaseClauseCommon(operation);

            base.VisitRelationalCaseClause(operation);
        }

        public override void VisitRangeCaseClause(IRangeCaseClause operation)
        {
            LogString(nameof(IRangeCaseClause));
            LogCaseClauseCommon(operation);

            Visit(operation.MinimumValue, "Min");
            Visit(operation.MaximumValue, "Max");
        }

1152 1153 1154 1155 1156 1157
        public override void VisitDefaultCaseClause(IDefaultCaseClause operation)
        {
            LogString(nameof(IDefaultCaseClause));
            LogCaseClauseCommon(operation);
        }

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
        public override void VisitInterpolatedStringExpression(IInterpolatedStringExpression operation)
        {
            LogString(nameof(IInterpolatedStringExpression));
            LogCommonPropertiesAndNewLine(operation);

            VisitArray(operation.Parts, "Parts", logElementCount: true);
        }

        public override void VisitInterpolatedStringText(IInterpolatedStringText operation)
        {
            LogString(nameof(IInterpolatedStringText));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Text, "Text");
        }

        public override void VisitInterpolation(IInterpolation operation)
        {
            LogString(nameof(IInterpolation));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Expression, "Expression");
            Visit(operation.Alignment, "Alignment");
            Visit(operation.FormatString, "FormatString");
        }

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
        public override void VisitConstantPattern(IConstantPattern operation)
        {
            LogString(nameof(IConstantPattern));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Value, "Value");
        }

        public override void VisitDeclarationPattern(IDeclarationPattern operation)
        {
            LogString(nameof(IDeclarationPattern));
            LogSymbol(operation.DeclaredSymbol, " (Declared Symbol");
            LogString(")");
            LogCommonPropertiesAndNewLine(operation);
        }

        public override void VisitIsPatternExpression(IIsPatternExpression operation)
        {
            LogString(nameof(IIsPatternExpression));
            LogCommonPropertiesAndNewLine(operation);

            Visit(operation.Expression, "Expression");
            Visit(operation.Pattern, "Pattern");
        }

        public override void VisitPatternCaseClause(IPatternCaseClause operation)
        {
            LogString(nameof(IPatternCaseClause));
            LogSymbol(operation.Label, " (Label Symbol");
            LogString(")");
M
Manish Vasani 已提交
1214
            LogCaseClauseCommon(operation);
1215 1216 1217 1218 1219

            Visit(operation.Pattern, "Pattern");
            Visit(operation.GuardExpression, "Guard Expression");
        }

1220 1221
        #endregion
    }
S
Sam Harwell 已提交
1222
}