Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpFeatureCall"/> class.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="source">Details of the feature call.</param>
        public CSharpFeatureCall(ICSharpContext context, IFeatureCall source)
        {
            foreach (IParameter Item in source.ParameterList)
            {
                ICSharpClass     Owner        = context.GetClass(Item.ResolvedParameter.Location.EmbeddingClass);
                ICSharpParameter NewParameter = CSharpParameter.Create(context, Item, Owner);
                ParameterList.Add(NewParameter);
            }

            foreach (IParameter Item in source.ResultList)
            {
                ICSharpClass     Owner        = context.GetClass(Item.ResolvedParameter.Location.EmbeddingClass);
                ICSharpParameter NewParameter = CSharpParameter.Create(context, Item, Owner);
                ResultList.Add(NewParameter);
            }

            foreach (IArgument Item in source.ArgumentList)
            {
                ICSharpArgument NewArgument = CSharpArgument.Create(context, Item);
                ArgumentList.Add(NewArgument);
            }

            Debug.Assert(ParameterList.Count >= ArgumentList.Count);
            Count = ParameterList.Count;

            ArgumentStyle = source.TypeArgumentStyle;
        }
Beispiel #2
0
        private static void CSharpAssignmentArgumentList(ICSharpWriter writer, ICSharpExpressionContext expressionContext, ICSharpFeatureCall featureCall, int skippedIndex, bool isAgentCall, out string callText, out IList <string> outgoingResultList)
        {
            IList <ICSharpParameter> ParameterList = featureCall.ParameterList;
            IList <ICSharpParameter> ResultList    = featureCall.ResultList;
            IList <ICSharpArgument>  ArgumentList  = featureCall.ArgumentList;

            int i;

            callText = string.Empty;

            for (i = 0; i < ParameterList.Count; i++)
            {
                if (callText.Length > 0)
                {
                    callText += ", ";
                }

                ICSharpParameter Parameter     = ParameterList[i];
                string           ParameterName = Parameter.Name;

                ICSharpExpression SourceExpression = null;

                foreach (ICSharpAssignmentArgument Argument in ArgumentList)
                {
                    foreach (string Name in Argument.ParameterNameList)
                    {
                        if (ParameterName == Name)
                        {
                            SourceExpression = Argument.SourceExpression;
                            break;
                        }
                    }
                }

                if (SourceExpression != null)
                {
                    ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();

                    SourceExpression.WriteCSharp(writer, SourceExpressionContext, -1);

                    callText += SourceExpressionContext.ResultListAsArgument;
                }
                else
                {
                    ICSharpScopeAttributeFeature Feature      = Parameter.Feature;
                    ICSharpExpression            DefaultValue = Feature.DefaultValue;

                    Debug.Assert(DefaultValue != null);

                    ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();
                    DefaultValue.WriteCSharp(writer, SourceExpressionContext, -1);

                    callText += SourceExpressionContext.ResultListAsArgument;
                }
            }

            CSharpAssignmentResultList(writer, expressionContext, featureCall, skippedIndex, isAgentCall, ref callText, out outgoingResultList);
        }
Beispiel #3
0
        private static void CSharpPositionalArgumentList(ICSharpWriter writer, ICSharpExpressionContext expressionContext, ICSharpFeatureCall featureCall, int skippedIndex, bool isAgentCall, out string callText, out IList <string> outgoingResultList)
        {
            IList <ICSharpParameter> ParameterList = featureCall.ParameterList;
            IList <ICSharpParameter> ResultList    = featureCall.ResultList;
            IList <ICSharpArgument>  ArgumentList  = featureCall.ArgumentList;

            int i, j;

            callText = string.Empty;

            i = 0;
            j = 0;
            for (; i < ArgumentList.Count; i++)
            {
                if (callText.Length > 0)
                {
                    callText += ", ";
                }

                ICSharpPositionalArgument Argument = ArgumentList[i] as ICSharpPositionalArgument;
                Debug.Assert(Argument != null);

                ICSharpExpression        SourceExpression        = Argument.SourceExpression;
                ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();

                SourceExpression.WriteCSharp(writer, SourceExpressionContext, -1);

                callText += SourceExpressionContext.ResultListAsArgument;

                j += SourceExpressionContext.CompleteDestinationNameList.Count;
                if (SourceExpressionContext.ReturnValue != null)
                {
                    j++;
                }
            }

            i = j;
            for (; i < ParameterList.Count; i++)
            {
                if (callText.Length > 0)
                {
                    callText += ", ";
                }

                ICSharpParameter             Parameter    = ParameterList[i];
                ICSharpScopeAttributeFeature Feature      = Parameter.Feature;
                ICSharpExpression            DefaultValue = Feature.DefaultValue;

                Debug.Assert(DefaultValue != null);

                ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();
                DefaultValue.WriteCSharp(writer, SourceExpressionContext, -1);

                callText += SourceExpressionContext.ResultListAsArgument;
            }

            CSharpAssignmentResultList(writer, expressionContext, featureCall, skippedIndex, isAgentCall, ref callText, out outgoingResultList);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CSharpCommandOverloadType"/> class.
 /// </summary>
 /// <param name="context">The creation context.</param>
 /// <param name="source">The Easly node from which the C# node is created.</param>
 /// <param name="owner">The class where the overload is declared.</param>
 protected CSharpCommandOverloadType(ICSharpContext context, ICommandOverloadType source, ICSharpClass owner)
     : base(source)
 {
     foreach (IParameter Parameter in source.ParameterTable)
     {
         ICSharpParameter NewParameter = CSharpParameter.Create(context, Parameter, owner);
         ParameterList.Add(NewParameter);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Builds a list of parameters, with and without their type.
        /// </summary>
        /// <param name="usingCollection">The collection of using directives.</param>
        /// <param name="parameterList">The list of parameters.</param>
        /// <param name="resultList">The list of results.</param>
        /// <param name="featureTextType">The write mode.</param>
        /// <param name="parameterListText">The list of parameters with type upon return.</param>
        /// <param name="parameterNameListText">The list of parameters without type upon return.</param>
        /// <param name="resultTypeText">The type text upon return.</param>
        public static void BuildParameterList(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> parameterList, IList <ICSharpParameter> resultList, CSharpFeatureTextTypes featureTextType, out string parameterListText, out string parameterNameListText, out string resultTypeText)
        {
            parameterListText     = string.Empty;
            parameterNameListText = string.Empty;

            foreach (ICSharpParameter Parameter in parameterList)
            {
                if (parameterListText.Length > 0)
                {
                    parameterListText += ", ";
                }
                if (parameterNameListText.Length > 0)
                {
                    parameterNameListText += ", ";
                }

                string      ParameterName = Parameter.Name;
                ICSharpType ParameterType = Parameter.Feature.Type;

                CSharpTypeFormats ParameterFormat = ParameterType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;

                string ParameterText     = ParameterType.Type2CSharpString(usingCollection, ParameterFormat, CSharpNamespaceFormats.None);
                string ParameterNameText = CSharpNames.ToCSharpIdentifier(ParameterName);

                parameterListText     += $"{ParameterText} {ParameterNameText}";
                parameterNameListText += ParameterNameText;
            }

            if (resultList.Count == 1)
            {
                BuildResultListSingle(usingCollection, resultList, out resultTypeText);
            }
            else
            {
                int ResultIndex = -1;
                for (int i = 0; i < resultList.Count; i++)
                {
                    ICSharpParameter Result = resultList[i];
                    if (Result.Name == nameof(BaseNode.Keyword.Result))
                    {
                        ResultIndex = i;
                        break;
                    }
                }

                if (ResultIndex < 0)
                {
                    BuildResultListNoResult(usingCollection, resultList, ref parameterListText, ref parameterNameListText, out resultTypeText);
                }
                else
                {
                    BuildResultListWithResult(usingCollection, resultList, ResultIndex, ref parameterListText, ref parameterNameListText, out resultTypeText);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpCommandOverload"/> class.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="source">The Easly node from which the C# node is created.</param>
        /// <param name="parentFeature">The parent feature.</param>
        /// <param name="owner">The class where the overload is declared.</param>
        protected CSharpCommandOverload(ICSharpContext context, ICommandOverload source, ICSharpFeature parentFeature, ICSharpClass owner)
            : base(source)
        {
            ParentFeature = parentFeature;

            foreach (IParameter Parameter in source.ParameterTable)
            {
                ICSharpParameter NewParameter = CSharpParameter.Create(context, Parameter, owner);
                ParameterList.Add(NewParameter);
            }

            Body = CSharpBody.Create(context, parentFeature, source.ResolvedBody.Item);
        }
Beispiel #7
0
        private static void BuildResultListSingle(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> resultList, out string resultTypeText)
        {
            Debug.Assert(resultList.Count == 1);

            ICSharpParameter             Result          = resultList[0];
            ICSharpScopeAttributeFeature ResultAttribute = Result.Feature;

            /*if (FeatureTextType == FeatureTextTypes.Interface)
             *  ResultType = CSharpTypes.Type2CSharpString(ResultAttribute.ResolvedFeatureType.Item, Context, CSharpTypeFormats.AsInterface);
             * else
             *  ResultType = CSharpTypes.Type2CSharpString(ResultAttribute.ResolvedFeatureType.Item, Context, CSharpTypeFormats.None);*/
            resultTypeText = ResultAttribute.Type.Type2CSharpString(usingCollection, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
        }
        /// <summary>
        /// Gets the feature output format.
        /// </summary>
        /// <param name="outgoingParameterCount">The number of 'out' parameters upon return.</param>
        /// <param name="returnValueIndex">Index of the return value if the feature returns a value, -1 otherwise.</param>
        public virtual void GetOutputFormat(out int outgoingParameterCount, out int returnValueIndex)
        {
            outgoingParameterCount = ResultList.Count;
            returnValueIndex       = -1;

            for (int i = 0; i < ResultList.Count; i++)
            {
                ICSharpParameter Result = ResultList[i];
                if (Result.Name == nameof(BaseNode.Keyword.Result))
                {
                    Debug.Assert(returnValueIndex == -1);
                    returnValueIndex = i;
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpQueryOverloadType"/> class.
        /// </summary>
        /// <param name="context">The creation context.</param>
        /// <param name="source">The Easly node from which the C# node is created.</param>
        /// <param name="owner">The class where the overload is declared.</param>
        protected CSharpQueryOverloadType(ICSharpContext context, IQueryOverloadType source, ICSharpClass owner)
            : base(source)
        {
            foreach (IParameter Parameter in source.ParameterTable)
            {
                ICSharpParameter NewParameter = CSharpParameter.Create(context, Parameter, owner);
                ParameterList.Add(NewParameter);
            }

            foreach (IParameter Result in source.ResultTable)
            {
                ICSharpParameter NewResult = CSharpParameter.Create(context, Result, owner);
                ResultList.Add(NewResult);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Initializes the feature overloads and bodies.
        /// </summary>
        /// <param name="context">The initialization context.</param>
        public override void InitOverloadsAndBodies(ICSharpContext context)
        {
            EntityType = CSharpType.Create(context, Source.ResolvedEntityType.Item);

            foreach (IParameter Parameter in Source.ParameterTable)
            {
                ICSharpParameter NewParameter = CSharpParameter.Create(context, Parameter, Owner);
                IndexParameterList.Add(NewParameter);
            }

            if (Source.GetterBody.IsAssigned)
            {
                GetterBody = CSharpBody.Create(context, this, (ICompiledBody)Source.GetterBody.Item);
            }

            if (Source.SetterBody.IsAssigned)
            {
                SetterBody = CSharpBody.Create(context, this, (ICompiledBody)Source.SetterBody.Item);
            }
        }
Beispiel #11
0
        private static void BuildResultListWithResult(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> resultList, int resultIndex, ref string parameterListText, ref string parameterNameListText, out string resultTypeText)
        {
            resultTypeText = null;

            for (int i = 0; i < resultList.Count; i++)
            {
                ICSharpParameter             Result          = resultList[i];
                ICSharpScopeAttributeFeature ResultAttribute = Result.Feature;
                ICSharpType       ParameterType   = ResultAttribute.Type;
                CSharpTypeFormats ParameterFormat = ParameterType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;

                string TypeString      = ParameterType.Type2CSharpString(usingCollection, ParameterFormat, CSharpNamespaceFormats.None);
                string AttributeString = CSharpNames.ToCSharpIdentifier(Result.Name);

                if (i == resultIndex)
                {
                    resultTypeText = TypeString;
                }
                else
                {
                    if (parameterListText.Length > 0)
                    {
                        parameterListText += ", ";
                    }
                    if (parameterNameListText.Length > 0)
                    {
                        parameterNameListText += ", ";
                    }

                    parameterListText     += $"out {TypeString} {AttributeString}";
                    parameterNameListText += $"out {AttributeString}";
                }
            }

            Debug.Assert(resultTypeText != null);
        }
Beispiel #12
0
        /// <summary>
        /// Writes down the C# overload of a feature.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="featureTextType">The write mode.</param>
        /// <param name="isOverride">True if the feature is an override.</param>
        /// <param name="nameString">The composed feature name.</param>
        /// <param name="exportStatus">The feature export status.</param>
        /// <param name="isConstructor">True if the feature is a constructor.</param>
        /// <param name="isFirstFeature">True if the feature is the first in a list.</param>
        /// <param name="isMultiline">True if there is a separating line above.</param>
        public void WriteCSharp(ICSharpWriter writer, CSharpFeatureTextTypes featureTextType, bool isOverride, string nameString, CSharpExports exportStatus, bool isConstructor, ref bool isFirstFeature, ref bool isMultiline)
        {
            Debug.Assert(WriteDown);

            IList <ICSharpParameter> SelectedParameterList = ParameterList;
            IList <ICSharpParameter> SelectedResultList    = ResultList;

            if (isOverride && Precursor != null)
            {
                SelectedParameterList = Precursor.ParameterList;
                SelectedResultList    = Precursor.ResultList;
            }

            CSharpArgument.BuildParameterList(writer, SelectedParameterList, SelectedResultList, featureTextType, out string ArgumentEntityList, out string ArgumentNameList, out string ResultType);
            string ExportStatusText;

            if (featureTextType == CSharpFeatureTextTypes.Implementation)
            {
                bool IsHandled = false;

                switch (Body)
                {
                case ICSharpDeferredBody AsDeferredBody:
                    CSharpAssertion.WriteContract(writer, AsDeferredBody.RequireList, AsDeferredBody.EnsureList, CSharpContractLocations.Other, true, ref isFirstFeature, ref isMultiline);
                    ExportStatusText = CSharpNames.ComposedExportStatus(false, true, false, exportStatus);
                    writer.WriteIndentedLine($"{ExportStatusText} {ResultType} {nameString}({ArgumentEntityList});");
                    isMultiline = false;
                    IsHandled   = true;
                    break;

                case ICSharpEffectiveBody AsEffectiveBody:
                    CSharpAssertion.WriteContract(writer, AsEffectiveBody.RequireList, AsEffectiveBody.EnsureList, CSharpContractLocations.Other, true, ref isFirstFeature, ref isMultiline);

                    CSharpBodyFlags Flags                    = CSharpBodyFlags.MandatoryCurlyBrackets;
                    string          ResultString             = string.Empty;
                    List <string>   InitialisationStringList = new List <string>();

                    if (ResultList.Count == 1)
                    {
                        Flags |= CSharpBodyFlags.HasResult;
                        ICSharpParameter Result = ResultList[0];
                        ResultString = Result.Feature.Type.Type2CSharpString(writer, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);
                    }
                    else
                    {
                        if (ResultType != "void")
                        {
                            Flags       |= CSharpBodyFlags.HasResult;
                            ResultString = ResultType;
                        }

                        foreach (ICSharpParameter Item in ResultList)
                        {
                            string InitValueString;

                            ICSharpType ResultEntityType = Item.Feature.Type;

                            if (ResultEntityType is ICSharpClassType AsClassType)
                            {
                                // TODO: when the type inherit from Enumeration
                                if (AsClassType.Class.Source.ClassGuid == LanguageClasses.AnyOptionalReference.Guid)
                                {
                                    InitValueString = "new OptionalReference<>(null)";     // TODO
                                }
                                else if (AsClassType.Class.Source.ClassGuid == LanguageClasses.String.Guid)
                                {
                                    InitValueString = "\"\"";
                                }

                                else if (AsClassType.Class.Source.ClassGuid == LanguageClasses.Boolean.Guid)
                                {
                                    InitValueString = "false";
                                }

                                else if (AsClassType.Class.Source.ClassGuid == LanguageClasses.Character.Guid)
                                {
                                    InitValueString = "'\0'";
                                }

                                else if (AsClassType.Class.Source.ClassGuid == LanguageClasses.Number.Guid)
                                {
                                    InitValueString = "0";
                                }

                                else
                                {
                                    InitValueString = "null";
                                }
                            }

                            else
                            {
                                InitValueString = "null";     // TODO : tuples
                            }
                            string InitNameString = CSharpNames.ToCSharpIdentifier(Item.Name);

                            InitialisationStringList.Add($"{InitNameString} = {InitValueString};");
                        }
                    }

                    ExportStatusText = CSharpNames.ComposedExportStatus(isOverride, false, false, exportStatus);
                    writer.WriteIndentedLine($"{ExportStatusText} {ResultType} {nameString}({ArgumentEntityList})");

                    AsEffectiveBody.WriteCSharp(writer, Flags, ResultString, false, InitialisationStringList);
                    isMultiline = true;
                    IsHandled   = true;
                    break;

                case ICSharpPrecursorBody AsPrecursorBody:
                    if (isMultiline)
                    {
                        writer.WriteEmptyLine();
                    }

                    ExportStatusText = CSharpNames.ComposedExportStatus(true, false, false, exportStatus);
                    writer.WriteIndentedLine($"{ExportStatusText} {ResultType} {nameString}({ArgumentEntityList})");
                    writer.WriteIndentedLine("{");
                    writer.IncreaseIndent();
                    writer.WriteIndentedLine($"return base.{nameString}({ArgumentNameList});");
                    writer.DecreaseIndent();
                    writer.WriteIndentedLine("}");
                    isMultiline = true;
                    IsHandled   = true;
                    break;
                }

                Debug.Assert(IsHandled);
            }
            else
            {
                writer.WriteIndentedLine($"{ResultType} {nameString}({ArgumentEntityList});");
                isMultiline = false;
            }
        }
        /// <summary>
        /// Get the name of a type.
        /// </summary>
        /// <param name="usingCollection">The collection of using directives.</param>
        /// <param name="cSharpTypeFormat">The type format.</param>
        /// <param name="cSharpNamespaceFormat">The namespace format.</param>
        public override string Type2CSharpString(ICSharpUsingCollection usingCollection, CSharpTypeFormats cSharpTypeFormat, CSharpNamespaceFormats cSharpNamespaceFormat)
        {
            SetUsedInCode();

            string Result;

            if (OriginatingTypedef != null)
            {
                // TODO: detect delegate call parameters to select the proper overload
                string DelegateName = CSharpNames.ToCSharpIdentifier(OriginatingTypedef.Name);

                Result = QueryOverloadType2CSharpString(DelegateName, Source.OverloadList[0]);
            }
            else
            {
                ICSharpQueryOverloadType OverloadType = OverloadTypeList[0];

                string ActionArgumentText = BaseType.Type2CSharpString(usingCollection, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);

                foreach (ICSharpParameter Parameter in OverloadType.ParameterList)
                {
                    ICSharpType       ParameterType   = Parameter.Feature.Type;
                    CSharpTypeFormats ParameterFormat = ParameterType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;
                    string            ParameterText   = ParameterType.Type2CSharpString(usingCollection, ParameterFormat, CSharpNamespaceFormats.None);

                    ActionArgumentText += $", {ParameterText}";
                }

                Debug.Assert(OverloadType.ResultList.Count >= 1);

                if (OverloadType.ResultList.Count == 1)
                {
                    ICSharpParameter  Parameter    = OverloadType.ResultList[0];
                    ICSharpType       ResultType   = Parameter.Feature.Type;
                    CSharpTypeFormats ResultFormat = ResultType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;
                    string            ResultText   = ResultType.Type2CSharpString(usingCollection, ResultFormat, CSharpNamespaceFormats.None);

                    ActionArgumentText += $", {ResultText}";
                }
                else
                {
                    string FuncResultText = string.Empty;

                    foreach (ICSharpParameter Parameter in OverloadType.ResultList)
                    {
                        if (FuncResultText.Length > 0)
                        {
                            FuncResultText += ", ";
                        }

                        ICSharpType       ResultType   = Parameter.Feature.Type;
                        CSharpTypeFormats ResultFormat = ResultType.HasInterfaceText ? CSharpTypeFormats.AsInterface : CSharpTypeFormats.Normal;
                        string            ResultText   = ResultType.Type2CSharpString(usingCollection, ResultFormat, CSharpNamespaceFormats.None);

                        FuncResultText += $", {ResultText}";
                    }

                    ActionArgumentText += $", Tuple<{FuncResultText}>";
                }

                Result = $"Func<{ActionArgumentText}>";

                usingCollection.AddUsing(nameof(System));
            }

            return(Result);
        }
Beispiel #14
0
        private static void CSharpAssignmentResultList(ICSharpWriter writer, ICSharpExpressionContext expressionContext, ICSharpFeatureCall featureCall, int skippedIndex, bool isAgentCall, ref string callText, out IList <string> outgoingResultList)
        {
            IList <ICSharpParameter> ResultList = featureCall.ResultList;

            outgoingResultList = new List <string>();

            for (int i = 0; i < ResultList.Count; i++)
            {
                if (i == skippedIndex)
                {
                    continue;
                }

                if (!isAgentCall && callText.Length > 0)
                {
                    callText += ", ";
                }

                ICSharpVariableContext Destination = i < expressionContext.DestinationNameList.Count ? expressionContext.DestinationNameList[i] : null;
                string ResultText;

                if (Destination == null || !Destination.IsDeclared)
                {
                    Debug.Assert(i < ResultList.Count);
                    ICSharpParameter callTextParameter = ResultList[i];

                    string TempTypeText = callTextParameter.Feature.Type.Type2CSharpString(writer, CSharpTypeFormats.AsInterface, CSharpNamespaceFormats.None);

                    string TempText;
                    if (Destination != null)
                    {
                        TempText = Destination.Name;
                    }
                    else
                    {
                        TempText = ResultList[i].Name;
                    }

                    TempText = writer.GetTemporaryName(TempText);

                    if (!isAgentCall)
                    {
                        callText += $"out {TempTypeText} {TempText}";
                    }

                    ResultText = TempText;
                }
                else
                {
                    string DestinationText = Destination.Name;

                    if (!isAgentCall)
                    {
                        callText += $"out {DestinationText}";
                    }

                    ResultText = DestinationText;
                }

                outgoingResultList.Add(ResultText);
            }
        }