<commit_before>namespace AutoMapper.Execution
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using AutoMapper.Configuration;
    using static System.Linq.Expressions.Expression;
    using static Internal.ExpressionFactory;
    using static ExpressionBuilder;
    using System.Diagnostics;

    public class TypeMapPlanBuilder
    {
        private static readonly Expression<Func<AutoMapperMappingException>> CtorExpression =
            () => new AutoMapperMappingException(null, null, default(TypePair), null, null);

        private static readonly Expression<Action<ResolutionContext>> IncTypeDepthInfo =
            ctxt => ctxt.IncrementTypeDepth(default(TypePair));

        private static readonly Expression<Action<ResolutionContext>> DecTypeDepthInfo =
            ctxt => ctxt.DecrementTypeDepth(default(TypePair));

        private static readonly Expression<Func<ResolutionContext, int>> GetTypeDepthInfo =
            ctxt => ctxt.GetTypeDepth(default(TypePair));

        private readonly IConfigurationProvider _configurationProvider;
        private readonly ParameterExpression _destination;
        private readonly ParameterExpression _initialDestination;
        private readonly TypeMap _typeMap;

        public TypeMapPlanBuilder(IConfigurationProvider configurationProvider, TypeMap typeMap)
        {
            _configurationProvider = configurationProvider;
            _typeMap = typeMap;
            Source = Parameter(typeMap.SourceType, "src");
            _initialDestination = Parameter(typeMap.DestinationTypeToUse, "dest");
            Context = Parameter(typeof(ResolutionContext), "ctxt");
            _destination = Variable(_initialDestination.Type, "typeMapDestination");
        }

        public ParameterExpression Source { get; }

        public ParameterExpression Context { get; }

        public LambdaExpression CreateMapperLambda(HashSet<TypeMap> visitedTypeMaps)
        {
            if (_typeMap.SourceType.IsGenericTypeDefinition() ||
                _typeMap.DestinationTypeToUse.IsGenericTypeDefinition())
                return null;
            var customExpression = TypeConverterMapper() ??
                                   _typeMap.Substitution ?? _typeMap.CustomMapper ?? _typeMap.CustomProjection;
            if (customExpression != null)
                return Lambda(customExpression.ReplaceParameters(Source, _initialDestination, Context), Source,
                    _initialDestination, Context);

            CheckForCycles(visitedTypeMaps);

            var destinationFunc = CreateDestinationFunc(out bool constructorMapping);

            var assignmentFunc = CreateAssignmentFunc(destinationFunc, constructorMapping);

            var mapperFunc = CreateMapperFunc(assignmentFunc);

            var checkContext = CheckContext(_typeMap, Context);
            var lambaBody = checkContext != null ? new[] {checkContext, mapperFunc} : new[] {mapperFunc};

            return Lambda(Block(new[] {_destination}, lambaBody), Source, _initialDestination, Context);
        }

        private void CheckForCycles(HashSet<TypeMap> visitedTypeMaps)
        {
            if(_typeMap.PreserveReferences)
            {
                return;
            }
            if(visitedTypeMaps == null)
            {
                visitedTypeMaps = new HashSet<TypeMap>();
            }
            visitedTypeMaps.Add(_typeMap);
            var propertyTypeMaps =
                (from propertyTypeMap in
                (from pm in _typeMap.GetPropertyMaps() where pm.CanResolveValue() select ResolvePropertyTypeMap(pm))
                where propertyTypeMap != null && !propertyTypeMap.PreserveReferences
                select propertyTypeMap).Distinct();
            foreach (var propertyTypeMap in propertyTypeMaps)
            {
                if(visitedTypeMaps.Add(propertyTypeMap))
                {
                    propertyTypeMap.Seal(_configurationProvider, visitedTypeMaps);
                }
                else
                {
                    Debug.WriteLine($"Setting PreserveReferences: {_typeMap.SourceType} - {_typeMap.DestinationType}=>{propertyTypeMap.SourceType} - {propertyTypeMap.DestinationType}");
                    propertyTypeMap.PreserveReferences = true;
                }
            }
        }

        private TypeMap ResolvePropertyTypeMap(PropertyMap propertyMap)
        {
            if (propertyMap.SourceType == null)
                return null;
            var types = new TypePair(propertyMap.SourceType, propertyMap.DestinationPropertyType);
            var typeMap = _configurationProvider.ResolveTypeMap(types);
            if (typeMap == null)
            {
                var mapper = _configurationProvider.FindMapper(types) as IObjectMapperInfo;
                if (mapper != null)<commit_msg>cosmetic<commit_after>namespace AutoMapper.Execution
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using AutoMapper.Configuration;
    using static System.Linq.Expressions.Expression;
    using static Internal.ExpressionFactory;
    using static ExpressionBuilder;
    using System.Diagnostics;

    public class TypeMapPlanBuilder
    {
        private static readonly Expression<Func<AutoMapperMappingException>> CtorExpression =
            () => new AutoMapperMappingException(null, null, default(TypePair), null, null);

        private static readonly Expression<Action<ResolutionContext>> IncTypeDepthInfo =
            ctxt => ctxt.IncrementTypeDepth(default(TypePair));

        private static readonly Expression<Action<ResolutionContext>> DecTypeDepthInfo =
            ctxt => ctxt.DecrementTypeDepth(default(TypePair));

        private static readonly Expression<Func<ResolutionContext, int>> GetTypeDepthInfo =
            ctxt => ctxt.GetTypeDepth(default(TypePair));

        private readonly IConfigurationProvider _configurationProvider;
        private readonly ParameterExpression _destination;
        private readonly ParameterExpression _initialDestination;
        private readonly TypeMap _typeMap;

        public TypeMapPlanBuilder(IConfigurationProvider configurationProvider, TypeMap typeMap)
        {
            _configurationProvider = configurationProvider;
            _typeMap = typeMap;
            Source = Parameter(typeMap.SourceType, "src");
            _initialDestination = Parameter(typeMap.DestinationTypeToUse, "dest");
            Context = Parameter(typeof(ResolutionContext), "ctxt");
            _destination = Variable(_initialDestination.Type, "typeMapDestination");
        }

        public ParameterExpression Source { get; }

        public ParameterExpression Context { get; }

        public LambdaExpression CreateMapperLambda(HashSet<TypeMap> visitedTypeMaps)
        {
            if (_typeMap.SourceType.IsGenericTypeDefinition() ||
                _typeMap.DestinationTypeToUse.IsGenericTypeDefinition())
                return null;
            var customExpression = TypeConverterMapper() ??
                                   _typeMap.Substitution ?? _typeMap.CustomMapper ?? _typeMap.CustomProjection;
            if (customExpression != null)
                return Lambda(customExpression.ReplaceParameters(Source, _initialDestination, Context), Source,
                    _initialDestination, Context);

            CheckForCycles(visitedTypeMaps);

            var destinationFunc = CreateDestinationFunc(out bool constructorMapping);

            var assignmentFunc = CreateAssignmentFunc(destinationFunc, constructorMapping);

            var mapperFunc = CreateMapperFunc(assignmentFunc);

            var checkContext = CheckContext(_typeMap, Context);
            var lambaBody = checkContext != null ? new[] {checkContext, mapperFunc} : new[] {mapperFunc};

            return Lambda(Block(new[] {_destination}, lambaBody), Source, _initialDestination, Context);
        }

        private void CheckForCycles(HashSet<TypeMap> visitedTypeMaps)
        {
            if(_typeMap.PreserveReferences)
            {
                return;
            }
            if(visitedTypeMaps == null)
            {
                visitedTypeMaps = new HashSet<TypeMap>();
            }
            visitedTypeMaps.Add(_typeMap);
            var propertyTypeMaps =
                (from propertyTypeMap in
                (from pm in _typeMap.GetPropertyMaps() where pm.CanResolveValue() select ResolvePropertyTypeMap(pm))
                where propertyTypeMap != null && !propertyTypeMap.PreserveReferences
                select propertyTypeMap).Distinct();
            foreach (var propertyTypeMap in propertyTypeMaps)
            {
                if(visitedTypeMaps.Add(propertyTypeMap))
                {
                    propertyTypeMap.Seal(_configurationProvider, visitedTypeMaps);
                }
                else
                {
                    Debug.WriteLine($"Setting PreserveReferences: {_typeMap.SourceType} - {_typeMap.DestinationType} => {propertyTypeMap.SourceType} - {propertyTypeMap.DestinationType}");
                    propertyTypeMap.PreserveReferences = true;
                }
            }
        }

        private TypeMap ResolvePropertyTypeMap(PropertyMap propertyMap)
        {
            if (propertyMap.SourceType == null)
                return null;
            var types = new TypePair(propertyMap.SourceType, propertyMap.DestinationPropertyType);
            var typeMap = _configurationProvider.ResolveTypeMap(types);
            if (typeMap == null)
            {
                var mapper = _configurationProvider.FindMapper(types) as IObjectMapperInfo;
                if (mapper != null)
{'fraction_non_alphanumeric': 0.07416438077821802, 'fraction_numerical': 0.0, 'mean_word_length': 2.4601054481546574, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6263140', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2256, 'n_words': 478}