Ejemplo n.º 1
0
        private static string CreateCSharp(BaseSchemaMember rootNode)
        {
            if (ErrorContext.Current.Errors.Any())
            {
                throw new InvalidFbsFileException(ErrorContext.Current.Errors);
            }

            var tablesNeedingSerializers = new List <TableOrStructDefinition>();
            var rpcDefinitions           = new List <RpcDefinition>();

            FindItemsRequiringSecondCodePass(rootNode, tablesNeedingSerializers, rpcDefinitions);

            if (tablesNeedingSerializers.Count == 0 && rpcDefinitions.Count == 0)
            {
                // Hey, no serializers or RPCs. We're all done. Go ahead and return the code we already generated.
                CodeWriter tempWriter = new CodeWriter();
                rootNode.WriteCode(tempWriter, CodeWritingPass.SecondPass, rootNode.DeclaringFile, new Dictionary <string, string>());

                if (ErrorContext.Current.Errors.Any())
                {
                    throw new InvalidFbsFileException(ErrorContext.Current.Errors);
                }

                return(tempWriter.ToString());
            }

            // Compile the assembly so that we may generate serializers for the data contracts defined in this FBS file.;
            // Compile with firstpass here to include all data (even stuff from includes).
            CodeWriter writer = new CodeWriter();

            rootNode.WriteCode(writer, CodeWritingPass.FirstPass, rootNode.DeclaringFile, new Dictionary <string, string>());
            if (ErrorContext.Current.Errors.Any())
            {
                throw new InvalidFbsFileException(ErrorContext.Current.Errors);
            }

            string code = writer.ToString();

            var(assembly, _, _) = RoslynSerializerGenerator.CompileAssembly(code, true);

            Dictionary <string, string> generatedSerializers = new Dictionary <string, string>();

            foreach (var definition in tablesNeedingSerializers)
            {
                generatedSerializers[definition.FullName] = GenerateSerializerForType(assembly, definition);
            }

            writer = new CodeWriter();
            rootNode.WriteCode(writer, CodeWritingPass.SecondPass, rootNode.DeclaringFile, generatedSerializers);

            if (ErrorContext.Current.Errors.Any())
            {
                throw new InvalidFbsFileException(ErrorContext.Current.Errors);
            }

            string rawCode       = writer.ToString();
            string formattedCode = RoslynSerializerGenerator.GetFormattedText(rawCode);

            return(formattedCode);
        }
Ejemplo n.º 2
0
        private static string CreateCSharp(string fbsSchema, string inputHash)
        {
            BaseSchemaMember rootNode = ParseSyntax(fbsSchema, inputHash);

            if (ErrorContext.Current.Errors.Any())
            {
                throw new InvalidFbsFileException(ErrorContext.Current.Errors);
            }

            // Create the first pass of the code. This pass includes the data contracts from the FBS file.
            // If the schema requests a pregenerated serializer, then we'll need to load this code, generate
            // the serializer, and then rebuild it.
            CodeWriter writer = new CodeWriter();

            rootNode.WriteCode(writer, CodeWritingPass.FirstPass, null);

            if (ErrorContext.Current.Errors.Any())
            {
                throw new InvalidFbsFileException(ErrorContext.Current.Errors);
            }

            string code = writer.ToString();

            var tablesNeedingSerializers = new List <TableOrStructDefinition>();
            var rpcDefinitions           = new List <RpcDefinition>();

            FindItemsRequiringSecondCodePass(rootNode, tablesNeedingSerializers, rpcDefinitions);

            if (tablesNeedingSerializers.Count == 0 && rpcDefinitions.Count == 0)
            {
                // Hey, no serializers or RPCs. We're all done. Go ahead and return the code we already generated.
                return(code);
            }

            // Compile the assembly so that we may generate serializers for the data contracts defined in this FBS file.
            var(assembly, _, _) = RoslynSerializerGenerator.CompileAssembly(code, true);

            Dictionary <string, string> generatedSerializers = new Dictionary <string, string>();

            foreach (var definition in tablesNeedingSerializers)
            {
                generatedSerializers[definition.FullName] = GenerateSerializerForType(assembly, definition);
            }

            writer = new CodeWriter();
            rootNode.WriteCode(writer, CodeWritingPass.SecondPass, generatedSerializers);

            if (ErrorContext.Current.Errors.Any())
            {
                throw new InvalidFbsFileException(ErrorContext.Current.Errors);
            }

            string rawCode       = writer.ToString();
            string formattedCode = RoslynSerializerGenerator.GetFormattedText(rawCode);

            return(formattedCode);
        }
Ejemplo n.º 3
0
        private static string CreateCSharp(BaseSchemaMember rootNode, CompilerOptions options)
        {
            ErrorContext.Current.ThrowIfHasErrors();

            if (string.IsNullOrEmpty(rootNode.DeclaringFile))
            {
                throw new InvalidFbsFileException("FlatSharp.Internal: RootNode missing declaring file");
            }

            Assembly?  assembly = null;
            CodeWriter writer   = new CodeWriter();
            var        steps    = new[]
            {
                CodeWritingPass.Initialization,
                CodeWritingPass.PropertyModeling,
                CodeWritingPass.SerializerGeneration,
                CodeWritingPass.RpcGeneration,
            };

            foreach (var step in steps)
            {
                var localOptions = options;

                if (step <= CodeWritingPass.PropertyModeling)
                {
                    localOptions = localOptions with {
                        NullableWarnings = false
                    };
                }

                if (step > CodeWritingPass.Initialization)
                {
                    string code = writer.ToString();
                    (assembly, _, _) = RoslynSerializerGenerator.CompileAssembly(code, true);
                }

                writer = new CodeWriter();

                rootNode.WriteCode(
                    writer,
                    new CompileContext
                {
                    CompilePass        = step,
                    Options            = localOptions,
                    RootFile           = rootNode.DeclaringFile,
                    PreviousAssembly   = assembly,
                    TypeModelContainer = TypeModelContainer.CreateDefault(),
                });

                ErrorContext.Current.ThrowIfHasErrors();
            }

            string rawCode       = writer.ToString();
            string formattedCode = RoslynSerializerGenerator.GetFormattedText(rawCode);

            return(formattedCode);
        }
    }