internal ThreadFlowLocation CreateThreadFlowLocation(AnnotatedCodeLocationVersionOne v1AnnotatedCodeLocation)
        {
            ThreadFlowLocation threadFlowLocation = null;

            if (v1AnnotatedCodeLocation != null)
            {
                threadFlowLocation = new ThreadFlowLocation
                {
                    Importance   = Utilities.CreateThreadFlowLocationImportance(v1AnnotatedCodeLocation.Importance),
                    Location     = CreateLocation(v1AnnotatedCodeLocation),
                    Module       = v1AnnotatedCodeLocation.Module,
                    NestingLevel = _threadFlowLocationNestingLevel,
                    Properties   = v1AnnotatedCodeLocation.Properties,
                    State        = v1AnnotatedCodeLocation.State,
                };

                if (v1AnnotatedCodeLocation.Kind == AnnotatedCodeLocationKindVersionOne.Call)
                {
                    _threadFlowLocationNestingLevel++;
                }
                else if (v1AnnotatedCodeLocation.Kind == AnnotatedCodeLocationKindVersionOne.CallReturn)
                {
                    _threadFlowLocationNestingLevel--;
                }
            }

            return(threadFlowLocation);
        }
        internal CodeFlowLocation CreateCodeFlowLocation(AnnotatedCodeLocationVersionOne v1AnnotatedCodeLocation)
        {
            CodeFlowLocation codeFlowLocation = null;

            if (v1AnnotatedCodeLocation != null)
            {
                codeFlowLocation = new CodeFlowLocation
                {
                    Importance   = Utilities.CreateCodeFlowLocationImportance(v1AnnotatedCodeLocation.Importance),
                    Location     = CreateLocation(v1AnnotatedCodeLocation),
                    Module       = v1AnnotatedCodeLocation.Module,
                    NestingLevel = _codeFlowLocationNestingLevel,
                    Properties   = v1AnnotatedCodeLocation.Properties,
                    State        = v1AnnotatedCodeLocation.State,
                    Step         = v1AnnotatedCodeLocation.Step + _codeFlowLocationStepAdjustment
                };

                if (v1AnnotatedCodeLocation.Kind == AnnotatedCodeLocationKindVersionOne.Call)
                {
                    _codeFlowLocationNestingLevel++;
                }
                else if (v1AnnotatedCodeLocation.Kind == AnnotatedCodeLocationKindVersionOne.CallReturn)
                {
                    _codeFlowLocationNestingLevel--;
                }
            }

            return(codeFlowLocation);
        }
        internal Location CreateLocation(AnnotatedCodeLocationVersionOne v1AnnotatedCodeLocation)
        {
            Location location = null;

            if (v1AnnotatedCodeLocation != null)
            {
                location = new Location
                {
                    Annotations = v1AnnotatedCodeLocation.Annotations?.SelectMany(a => a.Locations,
                                                                                  (a, pl) => CreateRegion(v1AnnotatedCodeLocation.PhysicalLocation,
                                                                                                          pl,
                                                                                                          a.Message))
                                  .Where(r => r != null)
                                  .ToList(),
                    FullyQualifiedLogicalName = v1AnnotatedCodeLocation.FullyQualifiedLogicalName,
                    Message          = CreateMessage(v1AnnotatedCodeLocation.Message),
                    PhysicalLocation = CreatePhysicalLocation(v1AnnotatedCodeLocation.PhysicalLocation),
                    Properties       = v1AnnotatedCodeLocation.Properties
                };

                string logicalLocationKey = v1AnnotatedCodeLocation.LogicalLocationKey ?? v1AnnotatedCodeLocation.FullyQualifiedLogicalName;

                if (!string.IsNullOrWhiteSpace(logicalLocationKey))
                {
                    if (_v1KeyToV2LogicalLocationMap.TryGetValue(logicalLocationKey, out LogicalLocation logicalLocation))
                    {
                        _v2LogicalLocationToIndexMap.TryGetValue(logicalLocation, out int index);
                        location.LogicalLocationIndex = index;
                    }
                }

                if (!string.IsNullOrWhiteSpace(v1AnnotatedCodeLocation.Snippet))
                {
                    if (location.PhysicalLocation == null)
                    {
                        location.PhysicalLocation = new PhysicalLocation();
                    }

                    if (location.PhysicalLocation.Region == null)
                    {
                        location.PhysicalLocation.Region = new Region();
                    }

                    location.PhysicalLocation.Region.Snippet = new ArtifactContent
                    {
                        Text = v1AnnotatedCodeLocation.Snippet
                    };
                }
            }

            return(location);
        }
        internal AnnotatedCodeLocationVersionOne CreateAnnotatedCodeLocationVersionOne(ThreadFlowLocation v2ThreadFlowLocation)
        {
            AnnotatedCodeLocationVersionOne annotatedCodeLocation = null;

            if (v2ThreadFlowLocation != null)
            {
                annotatedCodeLocation = CreateAnnotatedCodeLocationVersionOne(v2ThreadFlowLocation.Location);
                annotatedCodeLocation = annotatedCodeLocation ?? new AnnotatedCodeLocationVersionOne();

                annotatedCodeLocation.Importance = Utilities.CreateAnnotatedCodeLocationImportance(v2ThreadFlowLocation.Importance);
                annotatedCodeLocation.Module     = v2ThreadFlowLocation.Module;
                annotatedCodeLocation.Properties = v2ThreadFlowLocation.Properties;
                annotatedCodeLocation.State      = v2ThreadFlowLocation.State;
                annotatedCodeLocation.Step       = v2ThreadFlowLocation.ExecutionOrder;
            }

            return(annotatedCodeLocation);
        }
        internal AnnotatedCodeLocationVersionOne CreateAnnotatedCodeLocationVersionOne(Location v2Location)
        {
            AnnotatedCodeLocationVersionOne annotatedCodeLocation = null;

            if (v2Location != null)
            {
                annotatedCodeLocation = new AnnotatedCodeLocationVersionOne
                {
                    Annotations = v2Location.Annotations?.Select(CreateAnnotationVersionOne).ToList(),
                    FullyQualifiedLogicalName = v2Location.FullyQualifiedLogicalName,
                    Message          = v2Location.Message?.Text,
                    PhysicalLocation = CreatePhysicalLocationVersionOne(v2Location.PhysicalLocation),
                    Snippet          = v2Location.PhysicalLocation?.Region?.Snippet?.Text
                };
            }

            return(annotatedCodeLocation);
        }
        internal Location CreateLocation(AnnotatedCodeLocationVersionOne v1AnnotatedCodeLocation)
        {
            Location location = null;

            if (v1AnnotatedCodeLocation != null)
            {
                location = new Location
                {
                    Annotations = v1AnnotatedCodeLocation.Annotations?.SelectMany(a => a.Locations,
                                                                                  (a, pl) => CreateRegion(v1AnnotatedCodeLocation.PhysicalLocation,
                                                                                                          pl,
                                                                                                          a.Message))
                                  .Where(r => r != null)
                                  .ToList(),
                    FullyQualifiedLogicalName = v1AnnotatedCodeLocation.LogicalLocationKey ?? v1AnnotatedCodeLocation.FullyQualifiedLogicalName,
                    Message          = CreateMessage(v1AnnotatedCodeLocation.Message),
                    PhysicalLocation = CreatePhysicalLocation(v1AnnotatedCodeLocation.PhysicalLocation),
                    Properties       = v1AnnotatedCodeLocation.Properties
                };

                if (!string.IsNullOrWhiteSpace(v1AnnotatedCodeLocation.Snippet))
                {
                    if (location.PhysicalLocation == null)
                    {
                        location.PhysicalLocation = new PhysicalLocation();
                    }

                    if (location.PhysicalLocation.Region == null)
                    {
                        location.PhysicalLocation.Region = new Region();
                    }

                    location.PhysicalLocation.Region.Snippet = new FileContent
                    {
                        Text = v1AnnotatedCodeLocation.Snippet
                    };
                }
            }

            return(location);
        }
        public override AnnotatedCodeLocationVersionOne VisitAnnotatedCodeLocationVersionOne(AnnotatedCodeLocationVersionOne node)
        {
            if (!string.IsNullOrEmpty(node.LogicalLocationKey) &&
                !string.IsNullOrEmpty(node.FullyQualifiedLogicalName) &&
                !node.FullyQualifiedLogicalName.Equals(node.LogicalLocationKey))
            {
                LogicalLocationKeyToFullyQualifiedNameMap[node.LogicalLocationKey] = node.FullyQualifiedLogicalName;
            }

            // v1 annotated code location does not reference a decorated name

            return(base.VisitAnnotatedCodeLocationVersionOne(node));
        }