Esempio n. 1
0
        public static bool IsSame(ComputationContext ctx, FunctionDefinition derivedFunc,
                                  FunctionDefinition baseFunc, EntityInstance baseTemplate)
        {
            // todo: we have to check constraints as well
            if (!EntityNameArityComparer.Instance.Equals(derivedFunc.Name, baseFunc.Name))
            {
                return(false);
            }

            foreach (Tuple <TemplateParameter, TemplateParameter> param_pair in derivedFunc.Name.Parameters
                     .SyncZip(baseFunc.Name.Parameters))
            {
                if (!TemplateParameterExtension.IsSame(param_pair.Item1, param_pair.Item2, baseTemplate))
                {
                    return(false);
                }
            }

            {
                IEntityInstance base_result_type = baseFunc.ResultTypeName.Evaluation.Components.TranslateThrough(baseTemplate);
                if (!derivedFunc.ResultTypeName.Evaluation.Components.IsExactlySame(base_result_type, jokerMatchesAll: true))
                {
                    return(false);
                }
            }

            if (derivedFunc.Parameters.Count != baseFunc.Parameters.Count)
            {
                return(false);
            }

            foreach (Tuple <FunctionParameter, FunctionParameter> param_pair in derivedFunc.Parameters.SyncZip(baseFunc.Parameters))
            {
                if (!FunctionParameterExtension.IsSame(ctx, param_pair.Item1, param_pair.Item2, baseTemplate))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 2
0
        public static bool IsDerivedOf(ComputationContext ctx, FunctionDefinition derivedFunc,
                                       FunctionDefinition baseFunc, EntityInstance baseTemplate)
        {
            if (derivedFunc.IsPropertyAccessor(out Property derived_prop))
            {
                if (baseFunc.IsPropertyAccessor(out Property base_prop))
                {
                    // properties have to much and name (kind) of the accessor
                    if (!EntityNameArityComparer.Instance.Equals(derived_prop.Name, base_prop.Name) ||
                        !EntityNameArityComparer.Instance.Equals(derivedFunc.Name, baseFunc.Name))
                    {
                        return(false);
                    }
                }
                // property-getters can override regular methods
                else if (derived_prop.Getter != derivedFunc ||
                         !EntityNameArityComparer.Instance.Equals(derived_prop.Name, baseFunc.Name))
                {
                    return(false);
                }
            }
            // todo: we have to check constraints as well
            else if (!EntityNameArityComparer.Instance.Equals(derivedFunc.Name, baseFunc.Name))
            {
                return(false);
            }

            foreach (Tuple <TemplateParameter, TemplateParameter> param_pair in derivedFunc.Name.Parameters
                     .SyncZip(baseFunc.Name.Parameters))
            {
                if (!TemplateParameterExtension.IsSame(param_pair.Item1, param_pair.Item2, baseTemplate))
                {
                    return(false);
                }
            }

            {
                IEntityInstance base_result_type = baseFunc.ResultTypeName.Evaluation.Components.TranslateThrough(baseTemplate);
                TypeMatch       match            = derivedFunc.ResultTypeName.Evaluation.Components.MatchesTarget(ctx, base_result_type,
                                                                                                                  TypeMatching.Create(ctx.Env.Options.InterfaceDuckTyping, allowSlicing: false));
                if (match != TypeMatch.Same && match != TypeMatch.Substitute)
                {
                    return(false);
                }
            }

            if (derivedFunc.Parameters.Count != baseFunc.Parameters.Count)
            {
                return(false);
            }

            foreach (Tuple <FunctionParameter, FunctionParameter> param_pair in derivedFunc.Parameters.SyncZip(baseFunc.Parameters))
            {
                if (!FunctionParameterExtension.IsDerivedOf(ctx, param_pair.Item1, param_pair.Item2, baseTemplate))
                {
                    return(false);
                }
            }

            return(true);
        }