Example #1
0
        /// <summary>
        /// Sets the style of type arguments.
        /// </summary>
        /// <param name="argumentStyle">The style of type arguments.</param>
        public void SetArgumentStyle(TypeArgumentStyles argumentStyle)
        {
            Debug.Assert(ArgumentStyle == (TypeArgumentStyles)(-1));
            Debug.Assert(argumentStyle >= TypeArgumentStyles.None);

            ArgumentStyle = argumentStyle;
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FeatureCall"/> class.
 /// </summary>
 /// <param name="parameterList">List of parameters from the selected overload.</param>
 /// <param name="resultList">List of results from the selected overload.</param>
 /// <param name="argumentList">Arguments of the call.</param>
 /// <param name="resolvedArgumentList">Resolved arguments of the call.</param>
 /// <param name="typeArgumentStyle">The argument passing style.</param>
 public FeatureCall(IList <IParameter> parameterList, IList <IParameter> resultList, IList <IArgument> argumentList, IList <IExpressionType> resolvedArgumentList, TypeArgumentStyles typeArgumentStyle)
 {
     ParameterList        = parameterList;
     ResultList           = resultList;
     ArgumentList         = argumentList;
     ResolvedArgumentList = resolvedArgumentList;
     TypeArgumentStyle    = typeArgumentStyle;
 }
Example #3
0
        private static bool GetArgumentPassingStyle(IList <IArgument> argumentList, out TypeArgumentStyles argumentStyle, IErrorList errorList)
        {
            argumentStyle = TypeArgumentStyles.None;

            foreach (IArgument Argument in argumentList)
            {
                bool IsHandled = false;

                switch (Argument)
                {
                case IPositionalArgument AsPositionalArgument:
                    if (argumentStyle == TypeArgumentStyles.None)
                    {
                        argumentStyle = TypeArgumentStyles.Positional;
                    }
                    else if (argumentStyle == TypeArgumentStyles.Assignment)
                    {
                        errorList.AddError(new ErrorArgumentMixed(AsPositionalArgument));
                        return(false);
                    }

                    IsHandled = true;
                    break;

                case IAssignmentArgument AsAssignmentArgument:
                    if (argumentStyle == TypeArgumentStyles.None)
                    {
                        argumentStyle = TypeArgumentStyles.Assignment;
                    }
                    else if (argumentStyle == TypeArgumentStyles.Positional)
                    {
                        errorList.AddError(new ErrorArgumentMixed(AsAssignmentArgument));
                        return(false);
                    }

                    IsHandled = true;
                    break;
                }

                Debug.Assert(IsHandled);
            }

            return(true);
        }
Example #4
0
        private bool IsTypeArgumentValid(ITypeArgument item, ref TypeArgumentStyles argumentStyle)
        {
            bool Success   = true;
            bool IsHandled = false;

            if (item is IPositionalTypeArgument AsPositionalTypeArgument)
            {
                if (argumentStyle == TypeArgumentStyles.None)
                {
                    argumentStyle = TypeArgumentStyles.Positional;
                }
                else if (argumentStyle == TypeArgumentStyles.Assignment)
                {
                    AddSourceError(new ErrorTypeArgumentMixed(item));
                    Success = false;
                }

                IsHandled = true;
            }
            else if (item is IAssignmentTypeArgument AsAssignmentTypeArgument)
            {
                if (argumentStyle == TypeArgumentStyles.None)
                {
                    argumentStyle = TypeArgumentStyles.Assignment;
                }
                else if (argumentStyle == TypeArgumentStyles.Positional)
                {
                    AddSourceError(new ErrorTypeArgumentMixed(item));
                    Success = false;
                }

                IsHandled = true;
            }

            Debug.Assert(IsHandled);

            return(Success);
        }
Example #5
0
        /// <summary>
        /// Checks if actual arguments of a call conform to expected parameters.
        /// </summary>
        /// <param name="parameterTableList">The list of expected parameters.</param>
        /// <param name="arguments">The list of arguments.</param>
        /// <param name="argumentStyle">The argument-passing style.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="source">The source to use for errors.</param>
        /// <param name="selectedIndex">The selected index in the list of overloads upon return.</param>
        public static bool ArgumentsConformToParameters(IList <ISealableList <IParameter> > parameterTableList, IReadOnlyList <IExpressionType> arguments, TypeArgumentStyles argumentStyle, IErrorList errorList, ISource source, out int selectedIndex)
        {
            if (parameterTableList == null)
            {
                throw new ArgumentNullException(nameof(parameterTableList));
            }
            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }
            if (errorList == null)
            {
                throw new ArgumentNullException(nameof(errorList));
            }

            selectedIndex = -1;
            bool Result    = false;
            bool IsHandled = false;

            switch (argumentStyle)
            {
            case TypeArgumentStyles.None:
                Result    = NoneArgumentsConformToParameters(parameterTableList, arguments, errorList, source, out selectedIndex);
                IsHandled = true;
                break;

            case TypeArgumentStyles.Positional:
                Result    = PositionalArgumentsConformToParameters(parameterTableList, arguments, errorList, source, out selectedIndex);
                IsHandled = true;
                break;

            case TypeArgumentStyles.Assignment:
                Result    = AssignmentArgumentsConformToParameters(parameterTableList, arguments, errorList, source, out selectedIndex);
                IsHandled = true;
                break;
            }

            Debug.Assert(IsHandled);
            Debug.Assert(selectedIndex >= 0 || !Result);

            return(Result);
        }
Example #6
0
        /// <summary>
        /// Validate and merge a list of arguments.
        /// </summary>
        /// <param name="argumentList">The list of arguments.</param>
        /// <param name="mergedArgumentList">The merged result.</param>
        /// <param name="argumentStyle">The validated style.</param>
        /// <param name="errorList">List of errors found.</param>
        public static bool Validate(IList <IArgument> argumentList, List <IExpressionType> mergedArgumentList, out TypeArgumentStyles argumentStyle, IErrorList errorList)
        {
            if (argumentList == null)
            {
                throw new ArgumentNullException(nameof(argumentList));
            }
            if (mergedArgumentList == null)
            {
                throw new ArgumentNullException(nameof(mergedArgumentList));
            }
            if (errorList == null)
            {
                throw new ArgumentNullException(nameof(errorList));
            }

            argumentStyle = TypeArgumentStyles.None;

            if (!GetArgumentPassingStyle(argumentList, out argumentStyle, errorList))
            {
                return(false);
            }

            bool IsHandled = false;
            bool Success   = true;

            switch (argumentStyle)
            {
            case TypeArgumentStyles.None:
                IsHandled = true;
                break;

            case TypeArgumentStyles.Positional:
                Success   = ValidatePositionalStyle(argumentList, mergedArgumentList, errorList);
                IsHandled = true;
                break;

            case TypeArgumentStyles.Assignment:
                Success   = ValidateAssignmentStyle(argumentList, mergedArgumentList, errorList);
                IsHandled = true;
                break;
            }

            Debug.Assert(IsHandled);

            return(Success);
        }
        private static bool ResolveFeatureAsFunctionType(IQueryExpression node, IErrorList errorList, ICompiledFeature resolvedFinalFeature, List <IExpressionType> mergedArgumentList, TypeArgumentStyles typeArgumentStyle, IFunctionType finalType, ref ResolvedExpression resolvedExpression)
        {
            // IScopeAttributeFeature is the case of an agent.
            IList <IArgument>      ArgumentList            = node.ArgumentList;
            IFunctionFeature       AsFunctionFeature       = resolvedFinalFeature as IFunctionFeature;
            IScopeAttributeFeature AsScopeAttributeFeature = resolvedFinalFeature as IScopeAttributeFeature;

            Debug.Assert(AsFunctionFeature != null || AsScopeAttributeFeature != null);

            IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >();

            foreach (IQueryOverloadType Overload in finalType.OverloadList)
            {
                ParameterTableList.Add(Overload.ParameterTable);
            }

            int SelectedIndex;

            if (!Argument.ArgumentsConformToParameters(ParameterTableList, mergedArgumentList, typeArgumentStyle, errorList, node, out SelectedIndex))
            {
                return(false);
            }

            resolvedExpression.SelectedOverloadType = finalType.OverloadList[SelectedIndex];
            resolvedExpression.ResolvedResult       = new ResultType(resolvedExpression.SelectedOverloadType.ResultTypeList);
            resolvedExpression.ResolvedException    = new ResultException(resolvedExpression.SelectedOverloadType.ExceptionIdentifierList);

            if (AsFunctionFeature != null)
            {
                Debug.Assert(AsFunctionFeature.OverloadList.Count == finalType.OverloadList.Count);
                Debug.Assert(AsFunctionFeature.ResolvedAgentType.IsAssigned);
                Debug.Assert(AsFunctionFeature.ResolvedAgentType.Item == finalType);

                resolvedExpression.SelectedOverload = AsFunctionFeature.OverloadList[SelectedIndex];

                resolvedExpression.SelectedResultList = resolvedExpression.SelectedOverload.ResultTable;
                resolvedExpression.FeatureCall        = new FeatureCall(resolvedExpression.SelectedOverload.ParameterTable, resolvedExpression.SelectedOverload.ResultTable, ArgumentList, mergedArgumentList, typeArgumentStyle);
            }
            else
            {
                resolvedExpression.SelectedResultList = resolvedExpression.SelectedOverloadType.ResultTable;
                resolvedExpression.FeatureCall        = new FeatureCall(resolvedExpression.SelectedOverloadType.ParameterTable, resolvedExpression.SelectedOverloadType.ResultTable, ArgumentList, mergedArgumentList, typeArgumentStyle);
            }

            return(true);
        }
Example #8
0
        /// <summary>
        /// Checks for errors before applying a rule.
        /// </summary>
        /// <param name="node">The node instance to check.</param>
        /// <param name="dataList">Optional data collected during inspection of sources.</param>
        /// <param name="data">Private data to give to Apply() upon return.</param>
        /// <returns>True if an error occured.</returns>
        public override bool CheckConsistency(IGenericType node, IDictionary <ISourceTemplate, object> dataList, out object data)
        {
            bool Success = true;

            data = null;

            IIdentifier ClassIdentifier = (IIdentifier)node.ClassIdentifier;

            Debug.Assert(ClassIdentifier.ValidText.IsAssigned);
            string ValidIdentifier = ClassIdentifier.ValidText.Item;
            IClass EmbeddingClass  = node.EmbeddingClass;
            ISealableDictionary <string, IImportedClass> ImportedClassTable = EmbeddingClass.ImportedClassTable;

            ISealableDictionary <string, ICompiledType> ResolvedTable = new SealableDictionary <string, ICompiledType>();
            ISealableDictionary <string, IObjectType>   LocationTable = new SealableDictionary <string, IObjectType>();

            if (!ImportedClassTable.ContainsKey(ValidIdentifier))
            {
                AddSourceError(new ErrorUnknownIdentifier(ClassIdentifier, ValidIdentifier));
                Success = false;
            }
            else
            {
                TypeArgumentStyles ArgumentStyle = TypeArgumentStyles.None;

                foreach (ITypeArgument Item in node.TypeArgumentList)
                {
                    Success &= IsTypeArgumentValid(Item, ref ArgumentStyle);
                }

                IImportedClass Imported  = ImportedClassTable[ValidIdentifier];
                IClass         BaseClass = Imported.Item;

                if (BaseClass.GenericList.Count == 0)
                {
                    AddSourceError(new ErrorGenericClass(node, ValidIdentifier));
                    Success = false;
                }

                if (Success)
                {
                    bool IsHandled = false;

                    switch (ArgumentStyle)
                    {
                    case TypeArgumentStyles.None:
                    case TypeArgumentStyles.Positional:
                        Success   = CheckPositionalTypeArgumentsValidity(node, BaseClass, ResolvedTable, LocationTable);
                        IsHandled = true;
                        break;

                    case TypeArgumentStyles.Assignment:
                        Success   = CheckAssignmentTypeArgumentsValidity(node, BaseClass, ResolvedTable, LocationTable);
                        IsHandled = true;
                        break;
                    }

                    Debug.Assert(IsHandled);

                    if (Success)
                    {
                        ClassType.ResolveType(EmbeddingClass.TypeTable, BaseClass, ResolvedTable, EmbeddingClass.ResolvedClassType.Item, out ITypeName ValidResolvedTypeName, out ICompiledType ValidResolvedType);
                        data = new Tuple <IClass, TypeArgumentStyles, ISealableDictionary <string, ICompiledType>, ISealableDictionary <string, IObjectType>, ITypeName, ICompiledType>(BaseClass, ArgumentStyle, ResolvedTable, LocationTable, ValidResolvedTypeName, ValidResolvedType);
                    }
                }
            }

            return(Success);
        }
        private static bool ResolveCallProperty(IPrecursorExpression node, IFeatureInstance selectedPrecursor, IPropertyType callType, List <IExpressionType> mergedArgumentList, TypeArgumentStyles argumentStyle, IErrorList errorList, ref ResolvedExpression resolvedExpression, out ISealableList <IParameter> selectedParameterList, out ISealableList <IParameter> selectedResultList, out List <IExpressionType> resolvedArgumentList)
        {
            selectedParameterList = null;
            selectedResultList    = null;
            resolvedArgumentList  = null;

            IList <IArgument> ArgumentList = node.ArgumentList;

            if (ArgumentList.Count > 0)
            {
                errorList.AddError(new ErrorInvalidExpression(node));
                return(false);
            }
            else
            {
                ICompiledFeature OperatorFeature = selectedPrecursor.Feature;
                IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >();
                IPropertyFeature Property     = (IPropertyFeature)OperatorFeature;
                string           PropertyName = ((IFeatureWithName)Property).EntityName.Text;

                resolvedExpression.ResolvedResult = new ResultType(callType.ResolvedEntityTypeName.Item, callType.ResolvedEntityType.Item, PropertyName);

                resolvedExpression.ResolvedException = new ResultException();

                if (Property.GetterBody.IsAssigned)
                {
                    IBody GetterBody = (IBody)Property.GetterBody.Item;
                    resolvedExpression.ResolvedException = new ResultException(GetterBody.ExceptionIdentifierList);
                }

                selectedParameterList = new SealableList <IParameter>();
                selectedResultList    = new SealableList <IParameter>();
                resolvedArgumentList  = new List <IExpressionType>();

                return(true);
            }
        }
        private static bool ResolveCallFunction(IPrecursorExpression node, IFeatureInstance selectedPrecursor, IFunctionType callType, List <IExpressionType> mergedArgumentList, TypeArgumentStyles argumentStyle, IErrorList errorList, ref ResolvedExpression resolvedExpression, out ISealableList <IParameter> selectedParameterList, out ISealableList <IParameter> selectedResultList, out List <IExpressionType> resolvedArgumentList)
        {
            selectedParameterList = null;
            selectedResultList    = null;
            resolvedArgumentList  = null;

            IList <IArgument> ArgumentList    = node.ArgumentList;
            ICompiledFeature  OperatorFeature = selectedPrecursor.Feature;
            IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >();

            foreach (IQueryOverloadType Overload in callType.OverloadList)
            {
                ParameterTableList.Add(Overload.ParameterTable);
            }

            if (!Argument.ArgumentsConformToParameters(ParameterTableList, mergedArgumentList, argumentStyle, errorList, node, out int SelectedIndex))
            {
                return(false);
            }

            IFunctionFeature AsFunctionFeature = OperatorFeature as IFunctionFeature;

            Debug.Assert(AsFunctionFeature != null);
            Debug.Assert(AsFunctionFeature.OverloadList.Count == callType.OverloadList.Count);

            resolvedExpression.SelectedOverload     = AsFunctionFeature.OverloadList[SelectedIndex];
            resolvedExpression.SelectedOverloadType = callType.OverloadList[SelectedIndex];
            resolvedExpression.ResolvedResult       = new ResultType(resolvedExpression.SelectedOverloadType.ResultTypeList);
            resolvedExpression.ResolvedException    = new ResultException(resolvedExpression.SelectedOverloadType.ExceptionIdentifierList);
            selectedParameterList = resolvedExpression.SelectedOverloadType.ParameterTable;
            selectedResultList    = resolvedExpression.SelectedOverloadType.ResultTable;
            resolvedArgumentList  = mergedArgumentList;

            return(true);
        }
        private static bool ResolveCallClass(IPrecursorExpression node, IFeatureInstance selectedPrecursor, List <IExpressionType> mergedArgumentList, TypeArgumentStyles argumentStyle, IErrorList errorList, ref ResolvedExpression resolvedExpression, out ISealableList <IParameter> selectedParameterList, out ISealableList <IParameter> selectedResultList, out List <IExpressionType> resolvedArgumentList)
        {
            selectedParameterList = null;
            selectedResultList    = null;
            resolvedArgumentList  = null;

            IList <IArgument> ArgumentList = node.ArgumentList;

            if (ArgumentList.Count > 0)
            {
                errorList.AddError(new ErrorInvalidExpression(node));
                return(false);
            }
            else
            {
                ICompiledFeature OperatorFeature  = selectedPrecursor.Feature;
                ITypeName        OperatorTypeName = OperatorFeature.ResolvedEffectiveTypeName.Item;
                ICompiledType    OperatorType     = OperatorFeature.ResolvedEffectiveType.Item;
                IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >();

                resolvedExpression.ResolvedResult    = new ResultType(OperatorTypeName, OperatorType, string.Empty);
                resolvedExpression.ResolvedException = new ResultException();
                selectedParameterList = new SealableList <IParameter>();
                selectedResultList    = new SealableList <IParameter>();
                resolvedArgumentList  = new List <IExpressionType>();
            }

            return(true);
        }
        private static bool ResolveCall(IPrecursorExpression node, IFeatureInstance selectedPrecursor, List <IExpressionType> mergedArgumentList, TypeArgumentStyles argumentStyle, IErrorList errorList, ref ResolvedExpression resolvedExpression, out ISealableList <IParameter> selectedParameterList, out ISealableList <IParameter> selectedResultList, out List <IExpressionType> resolvedArgumentList)
        {
            selectedParameterList = null;
            selectedResultList    = null;
            resolvedArgumentList  = null;

            IList <IArgument> ArgumentList    = node.ArgumentList;
            ICompiledFeature  OperatorFeature = selectedPrecursor.Feature;
            IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >();
            bool IsHandled = false;
            bool Success   = false;

            switch (OperatorFeature)
            {
            case IAttributeFeature AsAttributeFeature:
            case ICreationFeature AsCreationFeature:
            case IProcedureFeature AsProcedureFeature:
            case IIndexerFeature AsIndexerFeature:
                errorList.AddError(new ErrorInvalidExpression(node));
                IsHandled = true;
                break;

            case IConstantFeature AsConstantFeature:
                Success   = ResolveCallClass(node, selectedPrecursor, mergedArgumentList, argumentStyle, errorList, ref resolvedExpression, out selectedParameterList, out selectedResultList, out resolvedArgumentList);
                IsHandled = true;
                break;

            case IFunctionFeature AsFunctionFeature:
                IFunctionType FunctionType = AsFunctionFeature.ResolvedAgentType.Item as IFunctionType;
                Debug.Assert(FunctionType != null);

                Success   = ResolveCallFunction(node, selectedPrecursor, FunctionType, mergedArgumentList, argumentStyle, errorList, ref resolvedExpression, out selectedParameterList, out selectedResultList, out resolvedArgumentList);
                IsHandled = true;
                break;

            case IPropertyFeature AsPropertyFeature:
                IPropertyType PropertyType = AsPropertyFeature.ResolvedAgentType.Item as IPropertyType;
                Debug.Assert(PropertyType != null);

                Success   = ResolveCallProperty(node, selectedPrecursor, PropertyType, mergedArgumentList, argumentStyle, errorList, ref resolvedExpression, out selectedParameterList, out selectedResultList, out resolvedArgumentList);
                IsHandled = true;
                break;
            }

            Debug.Assert(IsHandled);

            return(Success);
        }