Example #1
0
        /// <summary>
        /// Associates the given arguments with the template parameters specified in the type/method declarations
        /// and filters out unmatching overloads.
        /// </summary>
        /// <param name="rawOverloadList">Can be either type results or method results</param>
        /// <param name="givenTemplateArguments">A list of already resolved arguments passed explicitly
        /// in the !(...) section of a template instantiation
        /// or call arguments given in the (...) appendix
        /// that follows a method identifier</param>
        /// <param name="isMethodCall">If true, arguments that exceed the expected parameter count will be ignored as far as all parameters could be satisfied.</param>
        /// <param name="ctxt"></param>
        /// <returns>A filtered list of overloads which mostly fit to the specified arguments.
        /// Usually contains only 1 element.
        /// The 'TemplateParameters' property of the results will be also filled for further usage regarding smart completion etc.</returns>
        public static AbstractType[] DeduceParamsAndFilterOverloads(IEnumerable <AbstractType> rawOverloadList,
                                                                    IEnumerable <ISemantic> givenTemplateArguments,
                                                                    bool isMethodCall,
                                                                    ResolverContextStack ctxt)
        {
            if (rawOverloadList == null)
            {
                return(null);
            }

            var filteredOverloads = DeduceOverloads(rawOverloadList, givenTemplateArguments, isMethodCall, ctxt);

            AbstractType[] sortedAndFilteredOverloads = null;

            // If there are >1 overloads, filter from most to least specialized template param
            if (filteredOverloads.Count > 1)
            {
                sortedAndFilteredOverloads = SpecializationOrdering.FilterFromMostToLeastSpecialized(filteredOverloads, ctxt);
            }
            else if (filteredOverloads.Count == 1)
            {
                sortedAndFilteredOverloads = new[] { filteredOverloads[0] }
            }
            ;
            else
            {
                return(null);
            }

            if (sortedAndFilteredOverloads != null &&
                sortedAndFilteredOverloads.Length == 1 &&
                sortedAndFilteredOverloads[0] is TemplateType)
            {
                ImplicitTemplateProperties.TryGetImplicitProperty((TemplateType)sortedAndFilteredOverloads[0], ctxt, out sortedAndFilteredOverloads);
            }

            return(sortedAndFilteredOverloads);
        }
Example #2
0
        /// <summary>
        /// Associates the given arguments with the template parameters specified in the type/method declarations
        /// and filters out unmatching overloads.
        /// </summary>
        /// <param name="rawOverloadList">Can be either type results or method results</param>
        /// <param name="givenTemplateArguments">A list of already resolved arguments passed explicitly
        /// in the !(...) section of a template instantiation
        /// or call arguments given in the (...) appendix
        /// that follows a method identifier</param>
        /// <param name="isMethodCall">If true, arguments that exceed the expected parameter count will be ignored as far as all parameters could be satisfied.</param>
        /// <param name="ctxt"></param>
        /// <returns>A filtered list of overloads which mostly fit to the specified arguments.
        /// Usually contains only 1 element.
        /// The 'TemplateParameters' property of the results will be also filled for further usage regarding smart completion etc.</returns>
        public static AbstractType[] DeduceParamsAndFilterOverloads(IEnumerable <AbstractType> rawOverloadList,
                                                                    IEnumerable <ISemantic> givenTemplateArguments,
                                                                    bool isMethodCall,
                                                                    ResolutionContext ctxt,
                                                                    bool hasUndeterminedArguments = false)
        {
            if (rawOverloadList == null)
            {
                return(null);
            }

            var filteredOverloads = hasUndeterminedArguments ? new List <AbstractType>(rawOverloadList) :
                                    DeduceOverloads(rawOverloadList, givenTemplateArguments, isMethodCall, ctxt);

            AbstractType[] sortedAndFilteredOverloads;

            // If there are >1 overloads, filter from most to least specialized template param
            if (filteredOverloads.Count > 1)
            {
                sortedAndFilteredOverloads = SpecializationOrdering.FilterFromMostToLeastSpecialized(filteredOverloads, ctxt);
            }
            else if (filteredOverloads.Count == 1)
            {
                sortedAndFilteredOverloads = new[] { filteredOverloads[0] }
            }
            ;
            else
            {
                return(null);
            }

            if (hasUndeterminedArguments)
            {
                return(sortedAndFilteredOverloads);
            }

            if (sortedAndFilteredOverloads != null)
            {
                filteredOverloads.Clear();
                for (int i = sortedAndFilteredOverloads.Length - 1; i >= 0; i--)
                {
                    var ds = sortedAndFilteredOverloads[i] as DSymbol;
                    if (ds != null && ds.Definition.TemplateConstraint != null)
                    {
                        ctxt.CurrentContext.IntroduceTemplateParameterTypes(ds);
                        try{
                            var v = Evaluation.EvaluateValue(ds.Definition.TemplateConstraint, ctxt);
                            if (v is VariableValue)
                            {
                                v = new StandardValueProvider(ctxt)[((VariableValue)v).Variable];
                            }
                            if (!Evaluation.IsFalseZeroOrNull(v))
                            {
                                filteredOverloads.Add(ds);
                            }
                        }catch {}                        //TODO: Handle eval exceptions
                        ctxt.CurrentContext.RemoveParamTypesFromPreferredLocals(ds);
                    }
                    else
                    {
                        filteredOverloads.Add(sortedAndFilteredOverloads[i]);
                    }
                }
                if (filteredOverloads.Count == 0)
                {
                    return(null);
                }
                sortedAndFilteredOverloads = filteredOverloads.ToArray();
            }

            if (sortedAndFilteredOverloads != null &&
                sortedAndFilteredOverloads.Length == 1)
            {
                var t = sortedAndFilteredOverloads [0];
                if (t is TemplateType)
                {
                    return(TryGetImplicitProperty(t as TemplateType, ctxt) ?? sortedAndFilteredOverloads);
                }
                if (t is EponymousTemplateType)
                {
                    return new[] { DeduceEponymousTemplate(t as EponymousTemplateType, ctxt) }
                }
                ;
            }

            return(sortedAndFilteredOverloads);
        }