Beispiel #1
0
        /// <summary>
        /// Compares two overloads.
        /// </summary>
        /// <param name="overload1">The first overload.</param>
        /// <param name="overload2">The second overload.</param>
        public static bool QueryOverloadsHaveIdenticalSignature(IQueryOverloadType overload1, IQueryOverloadType overload2)
        {
            bool IsIdentical = true;

            IsIdentical &= overload1.ParameterList.Count == overload2.ParameterList.Count;
            IsIdentical &= overload1.ParameterEnd == overload2.ParameterEnd;
            IsIdentical &= overload1.ResultList.Count == overload2.ResultList.Count;

            for (int i = 0; i < overload1.ParameterList.Count && i < overload2.ParameterList.Count; i++)
            {
                Debug.Assert(overload1.ParameterList[i].ValidEntity.IsAssigned);
                Debug.Assert(overload1.ParameterList[i].ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
                Debug.Assert(overload2.ParameterList[i].ValidEntity.IsAssigned);
                Debug.Assert(overload2.ParameterList[i].ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
                IsIdentical &= ObjectType.TypesHaveIdenticalSignature(overload1.ParameterList[i].ValidEntity.Item.ResolvedEffectiveType.Item, overload2.ParameterList[i].ValidEntity.Item.ResolvedEffectiveType.Item);
            }

            for (int i = 0; i < overload1.ResultList.Count && i < overload2.ResultList.Count; i++)
            {
                Debug.Assert(overload1.ResultList[i].ValidEntity.IsAssigned);
                Debug.Assert(overload1.ResultList[i].ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
                Debug.Assert(overload2.ResultList[i].ValidEntity.IsAssigned);
                Debug.Assert(overload2.ResultList[i].ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
                IsIdentical &= ObjectType.TypesHaveIdenticalSignature(overload1.ResultList[i].ValidEntity.Item.ResolvedEffectiveType.Item, overload2.ResultList[i].ValidEntity.Item.ResolvedEffectiveType.Item);
            }

            IsIdentical &= Assertion.IsAssertionListEqual(overload1.RequireList, overload2.RequireList);
            IsIdentical &= Assertion.IsAssertionListEqual(overload1.EnsureList, overload2.EnsureList);
            IsIdentical &= ExceptionHandler.IdenticalExceptionSignature(overload1.ExceptionIdentifierList, overload2.ExceptionIdentifierList);

            return(IsIdentical);
        }
        /// <summary>
        /// Initializes the feature precursor hierarchy.
        /// </summary>
        /// <param name="context">The initialization context.</param>
        public override void InitHierarchy(ICSharpContext context)
        {
            if (Instance.OriginalPrecursor.IsAssigned)
            {
                IPrecursorInstance Item             = Instance.OriginalPrecursor.Item;
                ICompiledFeature   PrecursorFeature = Item.Precursor.Feature;

                OriginalPrecursor = context.GetFeature(PrecursorFeature) as ICSharpFunctionFeature;
                Debug.Assert(OriginalPrecursor != null);

                IList <ICSharpOverload> PrecursorOverloadList = OriginalPrecursor.OverloadList;

                foreach (ICSharpQueryOverload Overload in OverloadList)
                {
                    IQueryOverloadType   ResolvedAssociatedType  = Overload.Source.ResolvedAssociatedType.Item;
                    ICSharpQueryOverload ParentPrecursorOverload = null;

                    foreach (ICSharpQueryOverload PrecursorOverload in PrecursorOverloadList)
                    {
                        IQueryOverloadType PrecursorResolvedAssociatedType = PrecursorOverload.Source.ResolvedAssociatedType.Item;

                        if (ObjectType.QueryOverloadConformToBase(ResolvedAssociatedType, PrecursorResolvedAssociatedType, ErrorList.Ignored, ErrorList.NoLocation))
                        {
                            Debug.Assert(ParentPrecursorOverload == null);
                            ParentPrecursorOverload = PrecursorOverload;
                        }
                    }

                    Debug.Assert(ParentPrecursorOverload != null);
                    Overload.SetPrecursor(ParentPrecursorOverload);
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ErrorOverloadParameterMismatchConformance"/> class.
 /// </summary>
 /// <param name="source">The error location.</param>
 /// <param name="derivedType">The derived type</param>
 /// <param name="baseType">The base type.</param>
 public ErrorOverloadParameterMismatchConformance(ISource source, IQueryOverloadType derivedType, ICompiledType baseType)
     : base(source)
 {
     DerivedParameterList = derivedType.ParameterList;
     DerivedParameterEnd  = derivedType.ParameterEnd;
     BaseType             = baseType;
 }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpUnaryOperatorExpression"/> class.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="source">The Easly expression from which the C# expression is created.</param>
        protected CSharpUnaryOperatorExpression(ICSharpContext context, IUnaryOperatorExpression source)
            : base(context, source)
        {
            RightExpression = Create(context, (IExpression)source.RightExpression);

            Operator = context.GetFeature(source.SelectedFeature.Item) as ICSharpFunctionFeature;
            Debug.Assert(Operator != null);

            FeatureCall = new CSharpFeatureCall(context, new FeatureCall());

            IResultType     ResolvedRightResult  = RightExpression.Source.ResolvedResult.Item;
            IExpressionType PreferredRightResult = ResolvedRightResult.Preferred;

            Debug.Assert(PreferredRightResult != null);

            if (PreferredRightResult.ValueType is IClassType AsClassType)
            {
                if (AsClassType.BaseClass.ClassGuid == LanguageClasses.Number.Guid)
                {
                    IsCallingNumberFeature = true;
                }
            }

            if (IsCallingNumberFeature)
            {
                Debug.Assert(ResolvedRightResult.Count == 1);
            }

            Debug.Assert(Source.SelectedOverload.IsAssigned);
            IQueryOverload     Overload = Source.SelectedOverload.Item;
            IQueryOverloadType ResolvedAssociatedType = Overload.ResolvedAssociatedType.Item;

            SelectedOverloadType = CSharpQueryOverloadType.Create(context, ResolvedAssociatedType, Operator.Owner);
        }
Beispiel #5
0
        private static bool FunctionTypeConformToIndexerTypeOverloads(IQueryOverloadType derivedOverload, IIndexerType baseType, IErrorList errorList, ISource sourceLocation)
        {
            bool Result = true;

            Result &= derivedOverload.ParameterList.Count == baseType.IndexParameterList.Count && derivedOverload.ResultList.Count == 1 && derivedOverload.ParameterEnd != BaseNode.ParameterEndStatus.Closed;

            for (int i = 0; i < derivedOverload.ParameterList.Count && i < baseType.IndexParameterList.Count; i++)
            {
                IEntityDeclaration BaseParameter    = baseType.IndexParameterList[i];
                IEntityDeclaration DerivedParameter = derivedOverload.ParameterList[i];

                Debug.Assert(DerivedParameter.ValidEntity.IsAssigned);
                Debug.Assert(DerivedParameter.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
                Debug.Assert(BaseParameter.ValidEntity.IsAssigned);
                Debug.Assert(BaseParameter.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);

                Result &= TypeConformToBase(DerivedParameter.ValidEntity.Item.ResolvedEffectiveType.Item, BaseParameter.ValidEntity.Item.ResolvedEffectiveType.Item, errorList, sourceLocation, isConversionAllowed: false);
            }

            IEntityDeclaration OverloadResult = derivedOverload.ResultList[0];

            Debug.Assert(baseType.ResolvedEntityType.IsAssigned);
            Debug.Assert(OverloadResult.ValidEntity.IsAssigned);
            Debug.Assert(OverloadResult.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);

            Result &= TypeConformToBase(baseType.ResolvedEntityType.Item, OverloadResult.ValidEntity.Item.ResolvedEffectiveType.Item, ErrorList.Ignored, ErrorList.NoLocation, isConversionAllowed: false);
            Result &= ExceptionListConformToBase(derivedOverload.ExceptionIdentifierList, baseType.GetExceptionIdentifierList, errorList, sourceLocation);

            return(Result);
        }
Beispiel #6
0
        /// <summary>
        /// Creates an instance of a class type, or reuse an existing instance.
        /// </summary>
        /// <param name="instancingClassType">The class type to instanciate.</param>
        /// <param name="resolvedTypeName">The proposed type instance name.</param>
        /// <param name="resolvedType">The proposed type instance.</param>
        public void InstanciateType(ICompiledTypeWithFeature instancingClassType, ref ITypeName resolvedTypeName, ref ICompiledType resolvedType)
        {
            bool IsNewInstance = false;

            ITypeName     InstancedBaseTypeName = ResolvedBaseTypeName.Item;
            ICompiledType InstancedBaseType     = ResolvedBaseType.Item;

            InstancedBaseType.InstanciateType(instancingClassType, ref InstancedBaseTypeName, ref InstancedBaseType);

            IsNewInstance |= InstancedBaseType != ResolvedBaseType.Item;

            IList <IQueryOverloadType> InstancedOverloadList = new List <IQueryOverloadType>();

            foreach (IQueryOverloadType Overload in OverloadList)
            {
                IQueryOverloadType InstancedOverload = Overload;
                QueryOverloadType.InstanciateQueryOverloadType(instancingClassType, ref InstancedOverload);

                InstancedOverloadList.Add(InstancedOverload);
                IsNewInstance |= InstancedOverload != Overload;
            }

            if (IsNewInstance)
            {
                ISealableDictionary <ITypeName, ICompiledType> TypeTable = instancingClassType.GetTypeTable();
                ResolveType(TypeTable, InstancedBaseTypeName, (IObjectType)BaseType, (ICompiledTypeWithFeature)InstancedBaseType, InstancedOverloadList, out resolvedTypeName, out IFunctionType ResolvedFunctionType);
                resolvedType = ResolvedFunctionType;
            }
        }
Beispiel #7
0
        /// <summary>
        /// Gets the most common type of all overloads.
        /// </summary>
        /// <param name="overloadList">The list of overloads.</param>
        public static IResultType CommonResultType(IList <IQueryOverloadType> overloadList)
        {
            int MaxResult      = 0;
            int MaxResultIndex = 0;

            for (int i = 0; i < overloadList.Count; i++)
            {
                IQueryOverloadType Overload = overloadList[i];
                if (MaxResult < Overload.ResultTable.Count)
                {
                    MaxResult      = Overload.ResultTable.Count;
                    MaxResultIndex = i;
                }
            }

            IList <IExpressionType> ResultList = new List <IExpressionType>();

            for (int i = 0; i < MaxResult; i++)
            {
                GetCommonResultType(overloadList, i, out ITypeName ResultTypeName, out ICompiledType ResultType);

                Debug.Assert(i < overloadList[MaxResultIndex].ResultTypeList.Count);

                IExpressionType NewExpressionType = new ExpressionType(ResultTypeName, ResultType, overloadList[MaxResultIndex].ResultTypeList[i].Name);
                ResultList.Add(NewExpressionType);
            }

            return(new ResultType(ResultList));
        }
Beispiel #8
0
        private static bool QueryOverloadHasPropertyConformingBase(IQueryOverloadType derivedOverload, IPropertyType baseType, IErrorList errorList, ISource sourceLocation)
        {
            bool Result = true;

            if (derivedOverload.ParameterList.Count > 0 || derivedOverload.ResultList.Count != 1 || derivedOverload.ParameterEnd != BaseNode.ParameterEndStatus.Closed)
            {
                errorList.AddError(new ErrorOverloadParameterMismatchConformance(sourceLocation, derivedOverload, baseType));
                Result = false;
            }

            if (derivedOverload.ResultList.Count == 1)
            {
                IEntityDeclaration OverloadResult = derivedOverload.ResultList[0];

                Debug.Assert(baseType.ResolvedEntityType.IsAssigned);
                Debug.Assert(OverloadResult.ValidEntity.IsAssigned);
                Debug.Assert(OverloadResult.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);

                Result &= TypeConformToBase(baseType.ResolvedEntityType.Item, OverloadResult.ValidEntity.Item.ResolvedEffectiveType.Item, errorList, sourceLocation, isConversionAllowed: false);
            }

            Result &= ExceptionListConformToBase(derivedOverload.ExceptionIdentifierList, baseType.GetExceptionIdentifierList, errorList, sourceLocation);

            return(Result);
        }
        /// <summary>
        /// Applies the rule.
        /// </summary>
        /// <param name="node">The node instance to modify.</param>
        /// <param name="data">Private data from CheckConsistency().</param>
        public override void Apply(IQueryOverload node, object data)
        {
            Debug.Assert(node.ResolvedAssociatedType.IsAssigned);

            IQueryOverloadType OverloadType = node.ResolvedAssociatedType.Item;

            Debug.Assert(OverloadType.ConformantResultTable.IsSealed);
            node.CompleteConformantResultTable.AddRange(OverloadType.ConformantResultTable);
            node.CompleteConformantResultTable.Seal();
        }
Beispiel #10
0
        /// <summary>
        /// Applies the rule.
        /// </summary>
        /// <param name="node">The node instance to modify.</param>
        /// <param name="data">Private data from CheckConsistency().</param>
        public override void Apply(IQueryOverload node, object data)
        {
            node.ConformantResultTable.Seal();

            Debug.Assert(node.ResolvedAssociatedType.IsAssigned);
            IQueryOverloadType AssociatedType = node.ResolvedAssociatedType.Item;

            Debug.Assert(!AssociatedType.ConformantResultTable.IsSealed);
            AssociatedType.ConformantResultTable.Seal();
        }
Beispiel #11
0
        private static bool IndexerTypeConformToFunctionType(IIndexerType derivedType, IFunctionType baseType, IErrorList errorList, ISource sourceLocation)
        {
            bool Result = true;

            Debug.Assert(derivedType.ResolvedBaseType.IsAssigned);
            Debug.Assert(baseType.ResolvedBaseType.IsAssigned);

            Result &= TypeConformToBase(derivedType.ResolvedBaseType.Item, baseType.ResolvedBaseType.Item, errorList, sourceLocation, isConversionAllowed: false);

            if (derivedType.IndexerKind == BaseNode.UtilityType.WriteOnly)
            {
                errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType));
                Result = false;
            }

            if (baseType.OverloadList.Count > 1)
            {
                errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType));
                Result = false;
            }

            IQueryOverloadType SingleOverload = baseType.OverloadList[0];

            if (SingleOverload.ParameterList.Count != derivedType.IndexParameterList.Count || SingleOverload.ResultList.Count != 1 || SingleOverload.ParameterEnd != derivedType.ParameterEnd)
            {
                errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType));
                Result = false;
            }

            for (int i = 0; i < SingleOverload.ParameterList.Count && i < derivedType.IndexParameterList.Count; i++)
            {
                IEntityDeclaration BaseParameter    = SingleOverload.ParameterList[i];
                IEntityDeclaration DerivedParameter = derivedType.IndexParameterList[i];

                Debug.Assert(DerivedParameter.ValidEntity.IsAssigned);
                Debug.Assert(DerivedParameter.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
                Debug.Assert(BaseParameter.ValidEntity.IsAssigned);
                Debug.Assert(BaseParameter.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);

                Result &= TypeConformToBase(DerivedParameter.ValidEntity.Item.ResolvedEffectiveType.Item, BaseParameter.ValidEntity.Item.ResolvedEffectiveType.Item, errorList, sourceLocation, isConversionAllowed: false);
            }

            IEntityDeclaration OverloadResult = SingleOverload.ResultList[0];

            Debug.Assert(OverloadResult.ValidEntity.IsAssigned);
            Debug.Assert(OverloadResult.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
            Debug.Assert(derivedType.ResolvedEntityType.IsAssigned);

            Result &= TypeConformToBase(OverloadResult.ValidEntity.Item.ResolvedEffectiveType.Item, derivedType.ResolvedEntityType.Item, errorList, sourceLocation, isConversionAllowed: false);
            Result &= ExceptionListConformToBase(derivedType.GetExceptionIdentifierList, SingleOverload.ExceptionIdentifierList, errorList, sourceLocation);

            return(Result);
        }
Beispiel #12
0
        /// <summary>
        /// Applies the rule.
        /// </summary>
        /// <param name="node">The node instance to modify.</param>
        /// <param name="data">Private data from CheckConsistency().</param>
        public override void Apply(IUnaryOperatorExpression node, object data)
        {
            IResultType                 ResolvedResult       = ((Tuple <IResultType, IResultException, ISealableList <IExpression>, ILanguageConstant, IFunctionFeature, IQueryOverload, IQueryOverloadType>)data).Item1;
            IResultException            ResolvedException    = ((Tuple <IResultType, IResultException, ISealableList <IExpression>, ILanguageConstant, IFunctionFeature, IQueryOverload, IQueryOverloadType>)data).Item2;
            ISealableList <IExpression> ConstantSourceList   = ((Tuple <IResultType, IResultException, ISealableList <IExpression>, ILanguageConstant, IFunctionFeature, IQueryOverload, IQueryOverloadType>)data).Item3;
            ILanguageConstant           ExpressionConstant   = ((Tuple <IResultType, IResultException, ISealableList <IExpression>, ILanguageConstant, IFunctionFeature, IQueryOverload, IQueryOverloadType>)data).Item4;
            IFunctionFeature            SelectedFeature      = ((Tuple <IResultType, IResultException, ISealableList <IExpression>, ILanguageConstant, IFunctionFeature, IQueryOverload, IQueryOverloadType>)data).Item5;
            IQueryOverload              SelectedOverload     = ((Tuple <IResultType, IResultException, ISealableList <IExpression>, ILanguageConstant, IFunctionFeature, IQueryOverload, IQueryOverloadType>)data).Item6;
            IQueryOverloadType          SelectedOverloadType = ((Tuple <IResultType, IResultException, ISealableList <IExpression>, ILanguageConstant, IFunctionFeature, IQueryOverload, IQueryOverloadType>)data).Item7;

            node.ResolvedException.Item = ResolvedException;
        }
Beispiel #13
0
        /// <summary>
        /// Checks that a query overload conforms to another.
        /// </summary>
        /// <param name="derivedType">The derived type.</param>
        /// <param name="baseType">The base type.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="sourceLocation">The location for reporting errors.</param>
        public static bool QueryOverloadConformToBase(IQueryOverloadType derivedType, IQueryOverloadType baseType, IErrorList errorList, ISource sourceLocation)
        {
            bool Result = true;

            if (baseType.ParameterList.Count != derivedType.ParameterList.Count || baseType.ParameterEnd != derivedType.ParameterEnd)
            {
                errorList.AddError(new ErrorParameterMismatchConformance(sourceLocation, derivedType, baseType));
                Result = false;
            }

            if (baseType.ResultList.Count != derivedType.ResultList.Count)
            {
                errorList.AddError(new ErrorResultMismatchConformance(sourceLocation, derivedType, baseType));
                Result = false;
            }

            for (int i = 0; i < baseType.ParameterList.Count && i < derivedType.ParameterList.Count; i++)
            {
                IEntityDeclaration BaseParameter    = baseType.ParameterList[i];
                IEntityDeclaration DerivedParameter = derivedType.ParameterList[i];

                Debug.Assert(DerivedParameter.ValidEntity.IsAssigned);
                Debug.Assert(DerivedParameter.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
                Debug.Assert(BaseParameter.ValidEntity.IsAssigned);
                Debug.Assert(BaseParameter.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);

                Result &= TypeConformToBase(DerivedParameter.ValidEntity.Item.ResolvedEffectiveType.Item, BaseParameter.ValidEntity.Item.ResolvedEffectiveType.Item, errorList, sourceLocation, isConversionAllowed: false);
            }

            for (int i = 0; i < baseType.ResultList.Count && i < derivedType.ResultList.Count; i++)
            {
                IEntityDeclaration BaseResult    = baseType.ResultList[i];
                IEntityDeclaration DerivedResult = derivedType.ResultList[i];

                Debug.Assert(BaseResult.ValidEntity.IsAssigned);
                Debug.Assert(BaseResult.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);
                Debug.Assert(DerivedResult.ValidEntity.IsAssigned);
                Debug.Assert(DerivedResult.ValidEntity.Item.ResolvedEffectiveType.IsAssigned);

                Result &= TypeConformToBase(DerivedResult.ValidEntity.Item.ResolvedEffectiveType.Item, BaseResult.ValidEntity.Item.ResolvedEffectiveType.Item, errorList, sourceLocation, isConversionAllowed: false);
            }

            Result &= ExceptionListConformToBase(derivedType.ExceptionIdentifierList, baseType.ExceptionIdentifierList, errorList, sourceLocation);

            return(Result);
        }
Beispiel #14
0
        /// <summary>
        /// Checks if a matching function and overload exists in a type table.
        /// </summary>
        /// <param name="typeTable">The table of existing types.</param>
        /// <param name="overload">The overload to check.</param>
        /// <param name="overloadList">The list of other overloads in the candidate function type.</param>
        public static bool IsQueryOverloadMatching(ISealableDictionary <ITypeName, ICompiledType> typeTable, IQueryOverloadType overload, IList <IQueryOverloadType> overloadList)
        {
            bool IsMatching = false;

            for (int i = 0; i < overloadList.Count && !IsMatching; i++)
            {
                IQueryOverloadType Item = overloadList[i];

                IsMatching  = true;
                IsMatching &= IsParametersMatching(overload, Item);
                IsMatching &= IsResultsMatching(overload, Item);
                IsMatching &= Assertion.IsAssertionListEqual(overload.RequireList, Item.RequireList);
                IsMatching &= Assertion.IsAssertionListEqual(overload.EnsureList, Item.EnsureList);
                IsMatching &= ExceptionHandler.IdenticalExceptionSignature(overload.ExceptionIdentifierList, Item.ExceptionIdentifierList);
            }

            return(IsMatching);
        }
Beispiel #15
0
        private static bool IsResultsMatching(IQueryOverloadType overload1, IQueryOverloadType overload2)
        {
            bool IsMatching = true;

            IsMatching &= overload1.ResultList.Count == overload2.ResultList.Count;

            for (int i = 0; i < overload1.ResultList.Count && i < overload2.ResultList.Count; i++)
            {
                IScopeAttributeFeature OverloadAttribute1 = overload1.ResultList[i].ValidEntity.Item;
                IScopeAttributeFeature OverloadAttribute2 = overload2.ResultList[i].ValidEntity.Item;

                IsMatching &= OverloadAttribute1.ResolvedEffectiveType.Item == OverloadAttribute2.ResolvedEffectiveType.Item;
                IsMatching &= IsResultNameMatching(OverloadAttribute1.EntityName.Text, OverloadAttribute2.EntityName.Text);
                IsMatching &= IsDefaultValueMatching(OverloadAttribute1.DefaultValue, OverloadAttribute2.DefaultValue);
            }

            return(IsMatching);
        }
        /// <summary>
        /// Applies the rule.
        /// </summary>
        /// <param name="node">The node instance to modify.</param>
        /// <param name="data">Private data from CheckConsistency().</param>
        public override void Apply(IUnaryOperatorExpression node, object data)
        {
            ILanguageConstant ExpressionConstant = NeutralLanguageConstant.NotConstant;

            Debug.Assert(node.ConstantSourceList.Count == 1);

            IExpression RightConstantSource = node.ConstantSourceList[0];

            Debug.Assert(RightConstantSource == node.RightExpression);

            Debug.Assert(RightConstantSource.ExpressionConstant.IsAssigned);
            ILanguageConstant RightExpressionConstant = RightConstantSource.ExpressionConstant.Item;

            if (RightExpressionConstant != NeutralLanguageConstant.NotConstant)
            {
                Debug.Assert(node.SelectedOverloadType.IsAssigned);
                IQueryOverloadType SelectedOverloadType = node.SelectedOverloadType.Item;

                if (SelectedOverloadType.ResultTable.Count == 1)
                {
                    IParameter OverloadResult = SelectedOverloadType.ResultTable[0];
                    Debug.Assert(OverloadResult.ResolvedParameter.ResolvedEffectiveType.IsAssigned);

                    ICompiledType ResultType = OverloadResult.ResolvedParameter.ResolvedEffectiveType.Item;

                    if (Expression.IsLanguageTypeAvailable(LanguageClasses.Boolean.Guid, node, out ITypeName BooleanTypeName, out ICompiledType BooleanType) && ResultType == BooleanType)
                    {
                        ExpressionConstant = new BooleanLanguageConstant();
                    }
                    else if (Expression.IsLanguageTypeAvailable(LanguageClasses.Number.Guid, node, out ITypeName NumberTypeName, out ICompiledType NumberType) && ResultType == NumberType)
                    {
                        ExpressionConstant = new NumberLanguageConstant();
                    }
                    else if (Expression.IsLanguageTypeAvailable(LanguageClasses.Character.Guid, node, out ITypeName CharacterTypeName, out ICompiledType CharacterType) && ResultType == CharacterType)
                    {
                        ExpressionConstant = new CharacterLanguageConstant();
                    }
Beispiel #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ErrorResultMismatchConformance"/> class.
 /// </summary>
 /// <param name="source">The error location.</param>
 /// <param name="derivedType">The derived type</param>
 /// <param name="baseType">The base type.</param>
 public ErrorResultMismatchConformance(ISource source, IQueryOverloadType derivedType, IQueryOverloadType baseType)
     : base(source)
 {
     DerivedResultList = derivedType.ResultList;
     BaseResultList    = baseType.ResultList;
 }
Beispiel #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpBinaryOperatorExpression"/> class.
        /// </summary>
        /// <param name="source">The Easly expression from which the C# expression is created.</param>
        /// <param name="context">The creation context.</param>
        protected CSharpBinaryOperatorExpression(ICSharpContext context, IBinaryOperatorExpression source)
            : base(context, source)
        {
            LeftExpression  = Create(context, (IExpression)source.LeftExpression);
            RightExpression = Create(context, (IExpression)source.RightExpression);

            Operator = context.GetFeature(source.SelectedFeature.Item) as ICSharpFunctionFeature;
            Debug.Assert(Operator != null);

            IResultType     ResolvedLeftResult  = LeftExpression.Source.ResolvedResult.Item;
            IExpressionType PreferredLeftResult = ResolvedLeftResult.Preferred;

            Debug.Assert(PreferredLeftResult != null);

            if (PreferredLeftResult.ValueType is IClassType AsClassType)
            {
                if (AsClassType.BaseClass.ClassGuid == LanguageClasses.Number.Guid || AsClassType.BaseClass.ClassGuid == LanguageClasses.Integer.Guid)
                {
                    IsCallingNumberFeature = true;
                }
            }

            if (IsCallingNumberFeature)
            {
                IResultType ResolvedRightResult = RightExpression.Source.ResolvedResult.Item;
                Debug.Assert(ResolvedRightResult.Count == 1);
            }
            else
            {
                if (!LeftExpression.IsSingleResult)
                {
                    IResultType LeftResultType = LeftExpression.Source.ResolvedResult.Item;
                    LeftNameList        = new List <string>();
                    LeftResultNameIndex = LeftResultType.ResultNameIndex;

                    for (int i = 0; i < LeftResultType.Count; i++)
                    {
                        IExpressionType DestinationType = LeftResultType.At(i);
                        string          Text            = DestinationType.Name;
                        LeftNameList.Add(Text);
                    }
                }

                if (!RightExpression.IsSingleResult)
                {
                    IResultType RightResultType = RightExpression.Source.ResolvedResult.Item;
                    RightNameList        = new List <string>();
                    RightResultNameIndex = RightResultType.ResultNameIndex;

                    for (int i = 0; i < RightResultType.Count; i++)
                    {
                        IExpressionType DestinationType = RightResultType.At(i);
                        string          Text            = DestinationType.Name;
                        RightNameList.Add(Text);
                    }
                }
            }

            FeatureCall = new CSharpFeatureCall(context, Source.FeatureCall.Item);

            Debug.Assert(Source.SelectedOverload.IsAssigned);
            IQueryOverload     Overload = Source.SelectedOverload.Item;
            IQueryOverloadType ResolvedAssociatedType = Overload.ResolvedAssociatedType.Item;

            SelectedOverloadType = CSharpQueryOverloadType.Create(context, ResolvedAssociatedType, Operator.Owner);
        }
 private string QueryOverloadType2CSharpString(string delegateName, IQueryOverloadType overload)
 {
     // TODO
     return(delegateName);
 }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IBinaryOperatorExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        /// <param name="selectedFeature">The matching feature upon return.</param>
        /// <param name="selectedOverload">The matching overload in <paramref name="selectedFeature"/> upon return.</param>
        /// <param name="featureCall">Details of the feature call.</param>
        public static bool ResolveCompilerReferences(IBinaryOperatorExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out IFunctionFeature selectedFeature, out IQueryOverload selectedOverload, out IFeatureCall featureCall)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;
            selectedFeature    = null;
            selectedOverload   = null;
            featureCall        = null;

            IExpression LeftExpression  = (IExpression)node.LeftExpression;
            IIdentifier Operator        = (IIdentifier)node.Operator;
            IExpression RightExpression = (IExpression)node.RightExpression;
            IResultType LeftResult      = LeftExpression.ResolvedResult.Item;

            if (LeftResult.TryGetResult(out ICompiledType LeftExpressionType))
            {
                if (LeftExpressionType is IClassType AsClassType)
                {
                    string OperatorName = Operator.ValidText.Item;

                    ISealableDictionary <IFeatureName, IFeatureInstance> LeftFeatureTable = AsClassType.FeatureTable;

                    if (!FeatureName.TableContain(LeftFeatureTable, OperatorName, out IFeatureName Key, out IFeatureInstance Value))
                    {
                        errorList.AddError(new ErrorUnknownIdentifier(Operator, OperatorName));
                        return(false);
                    }

                    Debug.Assert(Value.Feature != null);

                    ICompiledFeature OperatorFeature = Value.Feature;
                    ICompiledType    OperatorType    = OperatorFeature.ResolvedAgentType.Item;

                    if (OperatorFeature is IFunctionFeature AsFunctionFeature && OperatorType is FunctionType AsFunctionType)
                    {
                        IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >();
                        foreach (IQueryOverloadType Overload in AsFunctionType.OverloadList)
                        {
                            ParameterTableList.Add(Overload.ParameterTable);
                        }

                        IResultType RightResult = RightExpression.ResolvedResult.Item;
                        if (!Argument.ArgumentsConformToParameters(ParameterTableList, RightResult.ToList(), TypeArgumentStyles.Positional, errorList, Operator, out int SelectedIndex))
                        {
                            return(false);
                        }

                        IQueryOverloadType SelectedOverloadType = AsFunctionType.OverloadList[SelectedIndex];
                        resolvedResult   = new ResultType(SelectedOverloadType.ResultTypeList);
                        selectedFeature  = AsFunctionFeature;
                        selectedOverload = AsFunctionFeature.OverloadList[SelectedIndex];

                        IArgument         FirstArgument = new PositionalArgument(RightExpression);
                        IList <IArgument> ArgumentList  = new List <IArgument>()
                        {
                            FirstArgument
                        };

                        List <IExpressionType> MergedArgumentList = new List <IExpressionType>();
                        bool IsArgumentValid = Argument.Validate(ArgumentList, MergedArgumentList, out TypeArgumentStyles TypeArgumentStyle, errorList);
                        Debug.Assert(IsArgumentValid);

                        featureCall       = new FeatureCall(SelectedOverloadType.ParameterTable, SelectedOverloadType.ResultTable, ArgumentList, MergedArgumentList, TypeArgumentStyle);
                        resolvedException = new ResultException(SelectedOverloadType.ExceptionIdentifierList);

                        constantSourceList.Add(LeftExpression);
                        constantSourceList.Add(RightExpression);
                    }
                    else
                    {
                        errorList.AddError(new ErrorInvalidOperator(Operator, OperatorName));
                        return(false);
                    }
                }
                else
                {
                    errorList.AddError(new ErrorInvalidExpression(LeftExpression));
                    return(false);
                }
            }
Beispiel #21
0
        /// <summary>
        /// Finds or creates an overload type with the corresponding parameters.
        /// </summary>
        /// <param name="instancingClassType">The type attempting to find the overload type.</param>
        /// <param name="instancedOverload">The new overload type upon return if not found.</param>
        public static void InstanciateQueryOverloadType(ICompiledTypeWithFeature instancingClassType, ref IQueryOverloadType instancedOverload)
        {
            bool IsNewInstance = false;

            IList <IEntityDeclaration> InstancedParameterList = new List <IEntityDeclaration>();

            foreach (IEntityDeclaration Parameter in instancedOverload.ParameterList)
            {
                ITypeName     InstancedParameterTypeName = Parameter.ValidEntity.Item.ResolvedEffectiveTypeName.Item;
                ICompiledType InstancedParameterType     = Parameter.ValidEntity.Item.ResolvedEffectiveType.Item;
                InstancedParameterType.InstanciateType(instancingClassType, ref InstancedParameterTypeName, ref InstancedParameterType);

                IEntityDeclaration InstancedParameter = new EntityDeclaration(Parameter, InstancedParameterTypeName, InstancedParameterType);
                IName ParameterName = (IName)Parameter.EntityName;

                IScopeAttributeFeature NewEntity;
                if (Parameter.DefaultValue.IsAssigned)
                {
                    // The default value has already been checked and validated.
                    bool IsCreated = ScopeAttributeFeature.Create(Parameter, ParameterName.ValidText.Item, InstancedParameterTypeName, InstancedParameterType, (IExpression)Parameter.DefaultValue.Item, ErrorList.Ignored, out NewEntity);
                    Debug.Assert(IsCreated);
                }
                else
                {
                    NewEntity = ScopeAttributeFeature.Create(Parameter, ParameterName.ValidText.Item, InstancedParameterTypeName, InstancedParameterType);
                }

                InstancedParameter.ValidEntity.Item = NewEntity;

                InstancedParameterList.Add(InstancedParameter);

                if (InstancedParameterType != Parameter.ValidEntity.Item.ResolvedEffectiveType.Item)
                {
                    IsNewInstance = true;
                }
            }

            IList <IEntityDeclaration> InstancedResultList = new List <IEntityDeclaration>();

            foreach (IEntityDeclaration Result in instancedOverload.ResultList)
            {
                ITypeName     InstancedResultTypeName = Result.ValidEntity.Item.ResolvedEffectiveTypeName.Item;
                ICompiledType InstancedResultType     = Result.ValidEntity.Item.ResolvedEffectiveType.Item;
                InstancedResultType.InstanciateType(instancingClassType, ref InstancedResultTypeName, ref InstancedResultType);

                IEntityDeclaration InstancedResult = new EntityDeclaration(Result, InstancedResultTypeName, InstancedResultType);
                IName ResultName = (IName)Result.EntityName;

                IScopeAttributeFeature NewEntity;
                if (Result.DefaultValue.IsAssigned)
                {
                    // The default value has already been checked and validated.
                    bool IsCreated = ScopeAttributeFeature.Create(Result, ResultName.ValidText.Item, InstancedResultTypeName, InstancedResultType, (IExpression)Result.DefaultValue.Item, ErrorList.Ignored, out NewEntity);
                    Debug.Assert(IsCreated);
                }
                else
                {
                    NewEntity = ScopeAttributeFeature.Create(Result, ResultName.ValidText.Item, InstancedResultTypeName, InstancedResultType);
                }

                InstancedResult.ValidEntity.Item = NewEntity;

                InstancedResultList.Add(InstancedResult);

                if (InstancedResultType != Result.ValidEntity.Item.ResolvedEffectiveType.Item)
                {
                    IsNewInstance = true;
                }
            }

            if (IsNewInstance)
            {
                IQueryOverloadType NewOverloadInstance = new QueryOverloadType(InstancedParameterList, instancedOverload.ParameterEnd, InstancedResultList, instancedOverload.RequireList, instancedOverload.EnsureList, instancedOverload.ExceptionIdentifierList);

                foreach (IEntityDeclaration Item in InstancedParameterList)
                {
                    string ValidName = Item.ValidEntity.Item.ValidFeatureName.Item.Name;
                    NewOverloadInstance.ParameterTable.Add(new Parameter(ValidName, Item.ValidEntity.Item));
                }

                foreach (IEntityDeclaration Item in InstancedResultList)
                {
                    string ValidName = Item.ValidEntity.Item.ValidFeatureName.Item.Name;
                    NewOverloadInstance.ResultTable.Add(new Parameter(ValidName, Item.ValidEntity.Item));
                }

                instancedOverload = NewOverloadInstance;
            }
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IUnaryOperatorExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        /// <param name="selectedFeature">The matching feature upon return.</param>
        /// <param name="selectedOverload">The matching overload in <paramref name="selectedFeature"/> upon return.</param>
        /// <param name="selectedOverloadType">The matching overload type upon return.</param>
        public static bool ResolveCompilerReferences(IUnaryOperatorExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out IFunctionFeature selectedFeature, out IQueryOverload selectedOverload, out IQueryOverloadType selectedOverloadType)
        {
            resolvedResult       = null;
            resolvedException    = null;
            constantSourceList   = new SealableList <IExpression>();
            expressionConstant   = NeutralLanguageConstant.NotConstant;
            selectedFeature      = null;
            selectedOverload     = null;
            selectedOverloadType = null;

            IIdentifier Operator        = (IIdentifier)node.Operator;
            string      ValidText       = Operator.ValidText.Item;
            IExpression RightExpression = (IExpression)node.RightExpression;
            IResultType RightResult     = RightExpression.ResolvedResult.Item;

            if (!RightResult.TryGetResult(out ICompiledType RightExpressionType))
            {
                errorList.AddError(new ErrorInvalidExpression(RightExpression));
                return(false);
            }

            if (RightExpressionType is IClassType AsClassType)
            {
                ISealableDictionary <IFeatureName, IFeatureInstance> RightFeatureTable = AsClassType.FeatureTable;

                if (!FeatureName.TableContain(RightFeatureTable, ValidText, out IFeatureName Key, out IFeatureInstance Value))
                {
                    errorList.AddError(new ErrorUnknownIdentifier(RightExpression, ValidText));
                    return(false);
                }

                ICompiledFeature OperatorFeature = Value.Feature;
                ICompiledType    OperatorType    = OperatorFeature.ResolvedAgentType.Item;

                if (OperatorFeature is IFunctionFeature AsFunctionFeature && OperatorType is IFunctionType AsFunctionType)
                {
                    IList <IQueryOverloadType> OperatorOverloadList = AsFunctionType.OverloadList;

                    int SelectedOperatorIndex = -1;
                    for (int i = 0; i < OperatorOverloadList.Count; i++)
                    {
                        IQueryOverloadType Overload = OperatorOverloadList[i];
                        if (Overload.ParameterList.Count == 0 && Overload.ResultList.Count == 1)
                        {
                            SelectedOperatorIndex = i;
                            break;
                        }
                    }

                    if (SelectedOperatorIndex < 0)
                    {
                        errorList.AddError(new ErrorInvalidOperator(Operator, ValidText));
                        return(false);
                    }

                    resolvedResult       = Feature.CommonResultType(AsFunctionType.OverloadList);
                    selectedFeature      = AsFunctionFeature;
                    selectedOverload     = AsFunctionFeature.OverloadList[SelectedOperatorIndex];
                    selectedOverloadType = OperatorOverloadList[SelectedOperatorIndex];
                    resolvedException    = new ResultException(selectedOverloadType.ExceptionIdentifierList);

                    constantSourceList.Add(RightExpression);
                }
                else
                {
                    errorList.AddError(new ErrorInvalidOperator(Operator, ValidText));
                    return(false);
                }
            }