예제 #1
0
        internal string GenerateMethodParamsStruct()
        {
            var code = new CodeBuilder();

            code.AddHeaderLine($"\tprivate struct {_methodInfo.MethodParamsStructName} {_methodInfo.GenericParams}");
            if (!string.IsNullOrEmpty(_methodInfo.ConstraintClauses))
            {
                code.AddHeaderLine($"\t\t{_methodInfo.ConstraintClauses}");
            }
            code.AddBlockHeader("\t");

            var paramsSymbols = _methodInfo.MethodSyntax.ParameterList.Parameters
                                .Select(parameterSyntax => (IParameterSymbol)_methodInfo.MethodModel.GetDeclaredSymbol(parameterSyntax))
                                .ToList();

            // fields
            code.AddHeaderLine(string.Join("\n",
                                           paramsSymbols.Select(paramSymbol => $"\t\tinternal readonly {paramSymbol.Type.ToDisplayString()} {paramSymbol.Name};")));
            code.AddHeaderLine();

            // constructor header
            code.AddBlockHeader($"\t\tinternal {_methodInfo.MethodParamsStructName}(" + string.Join(", ",
                                                                                                    paramsSymbols.Select(paramSymbol => $"{paramSymbol.Type.ToDisplayString()} {paramSymbol.Name}")) + ")");
            // constructor body
            code.AddHeaderLine(string.Join("\n",
                                           paramsSymbols.Select(paramSymbol => $"\t\t\tthis.{paramSymbol.Name} = {paramSymbol.Name};")));

            return(code.BuildString());
        }
예제 #2
0
        private string GenerateAll()
        {
            var allCode       = new CodeBuilder();
            var classInfo     = _classInfo;
            var classSyntax   = _classInfo.ContainingClassSyntax;
            var namespaceName = classInfo.ContainingClassSymbol.ContainingNamespace.ToDisplayString();

            var methodGens    = _methodInfos.Select(mi => new CalculatorMethodGenerator(_context, classInfo, mi)).ToList();
            var targetMethods = new TargetMethodsInfo(_methodInfos);

            // copy all the usings from the original file
            var root = (CompilationUnitSyntax)classSyntax.SyntaxTree.GetRoot();

            allCode.AddHeaderLine(root.Usings.ToFullString());
            allCode.AddHeaderLine();

            // namespace
            allCode.AddBlockHeader($"namespace {namespaceName}");

            // class declaration
            var classModifiersString = classSyntax.Modifiers.ToFullString();

            if (!classModifiersString.Contains("partial"))
            {
                _context.ReportUnsupportedSyntaxError(classSyntax, $"Target class '{_classInfo.ContainingClassSymbol.Name}' for '{classInfo.RecursionId}' is not partial");
                return(null);
            }

            allCode.AddBlockHeader($"{classModifiersString} class {_classInfo.ContainingClassSymbol.Name}");

            allCode.AddHeaderLine();
            allCode.AddHeaderLine(GenerateProxyMethods(methodGens));

            allCode.AddHeaderLine();
            allCode.AddHeaderLine(GenerateParamsStructs(classInfo, methodGens));

            allCode.AddHeaderLine();
            allCode.AddHeaderLine(GenerateCalculatorClass(classInfo, methodGens, targetMethods));

            return(allCode.BuildString());
        }
예제 #3
0
        private string GenerateCallSiteEnum(RecursiveCalculatorClassInfo classInfo, IReadOnlyList <CalculatorMethodGenerator> methodGens)
        {
            var code = new CodeBuilder();

            code.AddBlockHeader($"\tprivate enum {classInfo.CallSiteEnumName} : int");
            foreach (var methodGen in methodGens)
            {
                code.AddHeaderLine($"\t\t{methodGen.MethodInfo.MethodEnumValueName},");
            }

            return(code.BuildString());
        }
예제 #4
0
        private void GenerateProxyMethodBody(CodeBuilder code)
        {
            // method body
            code.AddBlockHeader("\t");
            // create the initial CallParams struct
            code.AddHeader($"\t\tvar {ParamsVarName} = new {_methodInfo.MethodParamsStructName}{_methodInfo.GenericParams} (")
            .AddHeader(string.Join(", ",
                                   _methodInfo.MethodSyntax.ParameterList.Parameters.Select(parameterSyntax => _methodInfo.MethodModel.GetDeclaredSymbol(parameterSyntax).Name)))
            .AddHeaderLine(");");

            code.AddHeaderLine($"\t\tvar runner = new {_calculatorClassInfo.CalculatorClassName}{_calculatorClassInfo.GenericParams}();");
            code.AddHeaderLine($"\t\trunner.RunRecursion({ParamsVarName});");
            if (_methodInfo.ShouldGenerateReturnField)
            {
                code.AddHeaderLine($"\t\treturn runner.{_methodInfo.ReturnFieldName};");
            }
        }
예제 #5
0
        private string GenerateDispatchStruct(RecursiveCalculatorClassInfo classInfo, IReadOnlyList <CalculatorMethodGenerator> methodGens)
        {
            var code = new CodeBuilder();

            // generic struct can't have an explicit layout so use just a simple class in such case instead
            bool isGeneric = !string.IsNullOrWhiteSpace(classInfo.GenericParams);

            if (!isGeneric)
            {
                code.AddHeaderLine("\t[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]");
                code.AddHeaderLine($"\tprivate struct {classInfo.ParamsStructName} {classInfo.GenericParams}");
            }
            else
            {
                code.AddHeaderLine($"\tprivate class {classInfo.ParamsStructName} {classInfo.GenericParams}");
            }

            if (!string.IsNullOrEmpty(classInfo.ConstraintClauses))
            {
                code.AddHeaderLine($"\t\t{classInfo.ConstraintClauses}");
            }
            code.AddBlockHeader("\t");

            // the call site dispatch enum field
            if (!isGeneric)
            {
                code.AddHeaderLine("\t\t[System.Runtime.InteropServices.FieldOffset(0)]");
            }
            code.AddHeaderLine($"\t\tinternal readonly {classInfo.CallSiteEnumName} {RecursiveCalculatorClassInfo.CallSiteEnumFieldName};");

            // params fields for each method
            foreach (var methodGen in methodGens)
            {
                code.AddHeaderLine();
                if (!isGeneric)
                {
                    code.AddHeaderLine($"\t\t[System.Runtime.InteropServices.FieldOffset(sizeof({classInfo.CallSiteEnumName}))]");
                }
                code.AddHeaderLine(
                    $"\t\tinternal readonly {methodGen.MethodInfo.MethodParamsStructName}{methodGen.MethodInfo.GenericParams} {methodGen.MethodInfo.MethodParamsStructName};");
            }

            // constructors: 1 for each method
            foreach (var methodGen in methodGens)
            {
                var methodInfo = methodGen.MethodInfo;
                // constructor header
                //TODO: improve CodeBuilder to use code.AddBlockHeader() here
                code.AddHeaderLine($"\t\tinternal {classInfo.ParamsStructName}({methodInfo.MethodParamsStructName}{methodInfo.GenericParams} {methodInfo.MethodParamsStructName})");
                if (!isGeneric)
                {
                    code.AddHeaderLine("\t\t\t:this()");
                }

                code.AddHeaderLine("\t\t{");
                // constructor body
                code.AddHeaderLine($"\t\t\tthis.{RecursiveCalculatorClassInfo.CallSiteEnumFieldName} = {classInfo.CallSiteEnumName}.{methodInfo.MethodEnumValueName};");
                code.AddHeaderLine($"\t\t\tthis.{methodInfo.MethodParamsStructName} = {methodInfo.MethodParamsStructName};");
                code.AddHeaderLine("\t\t}");

                // using an implicit operator is a small hack that simplifies code rewriting
                code.AddHeaderLine(
                    $"\t\tpublic static implicit operator {classInfo.ParamsStructName}{classInfo.GenericParams}({methodInfo.MethodParamsStructName}{methodInfo.GenericParams} {methodInfo.MethodParamsStructName}) "
                    + $" => new {classInfo.ParamsStructName}{classInfo.GenericParams}({methodInfo.MethodParamsStructName});");
            }

            return(code.BuildString());
        }