Example #1
0
        private static void BuildResultListNoResult(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> resultList, ref string parameterListText, ref string parameterNameListText, out string resultTypeText)
        {
            resultTypeText = "void";

            foreach (ICSharpParameter Result in resultList)
            {
                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 (parameterListText.Length > 0)
                {
                    parameterListText += ", ";
                }
                if (parameterNameListText.Length > 0)
                {
                    parameterNameListText += ", ";
                }

                parameterListText     += $"out {TypeString} {AttributeString}";
                parameterNameListText += $"out {AttributeString}";
            }
        }
Example #2
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="parameterListText">The list of parameters with type upon return.</param>
        /// <param name="parameterNameListText">The list of parameters without type upon return.</param>
        public static void BuildParameterList(ICSharpUsingCollection usingCollection, IList <ICSharpParameter> parameterList, out string parameterListText, out string parameterNameListText)
        {
            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;
            }
        }
        /// <summary>
        /// Gets the source code corresponding to the expression.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="expressionContext">The context.</param>
        /// <param name="skippedIndex">Index of a destination to skip.</param>
        public override void WriteCSharp(ICSharpWriter writer, ICSharpExpressionContext expressionContext, int skippedIndex)
        {
            Debug.Assert(WriteDown);

            Debug.Assert(TypeList.Count > 0);

            string CloneMethod = Source.Type == BaseNode.CloneType.Shallow ? "CloneShallow" : "Clone";

            ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();

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

            IList <string> ResultList       = new List <string>();
            int            ReturnValueIndex = SourceExpressionContext.ReturnValueIndex;
            string         ReturnValue      = SourceExpressionContext.ReturnValue;

            if (TypeList.Count == 1)
            {
                ICSharpType ClonedType     = TypeList[0];
                string      SourceTypeText = ClonedType.Type2CSharpString(writer, CSharpTypeFormats.Normal, CSharpNamespaceFormats.None);

                string SourceText = SourceExpressionContext.ReturnValue;
                Debug.Assert(SourceText != null);

                expressionContext.SetSingleReturnValue($"({SourceTypeText})({SourceText}).{CloneMethod}()");
            }
            else
            {
                for (int i = 0; i < SourceExpressionContext.CompleteDestinationNameList.Count; i++)
                {
                    if (i == ReturnValueIndex)
                    {
                        Debug.Assert(ReturnValue != null);
                        ResultList.Add(ReturnValue);
                    }
                    else
                    {
                        ResultList.Add(SourceExpressionContext.CompleteDestinationNameList[i]);
                    }
                }

                Debug.Assert(TypeList.Count == ResultList.Count);

                IList <string> OutgoingResultList = new List <string>();

                for (int i = 0; i < TypeList.Count; i++)
                {
                    ICSharpType ClonedType     = TypeList[i];
                    string      SourceTypeText = ClonedType.Type2CSharpString(writer, CSharpTypeFormats.Normal, CSharpNamespaceFormats.None);
                    string      SourceText     = ResultList[i];

                    OutgoingResultList.Add($"({SourceTypeText})({SourceText}).{CloneMethod}()");
                }

                expressionContext.SetMultipleResult(OutgoingResultList, ReturnValueIndex);
            }
        }
Example #4
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);
                }
            }
        }
Example #5
0
        /// <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;

            // TODO: detect delegate call parameters to select the proper overload

            if (OriginatingTypedef != null)
            {
                string DelegateName = CSharpNames.ToCSharpIdentifier(OriginatingTypedef.Name);

                Result = CommandOverloadType2CSharpString(DelegateName, Source.OverloadList[0]);
            }
            else
            {
                ICSharpCommandOverloadType 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}";
                }

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

                usingCollection.AddUsing(nameof(System));
            }

            return(Result);
        }
Example #6
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);
        }
        /// <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);
        }