Exemplo n.º 1
0
        protected override void OnWriteCode(CodeWriter writer, CodeWritingPass pass, IReadOnlyDictionary <string, string> precompiledSerailizers)
        {
            writer.AppendLine($@"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by the FlatSharp FBS to C# compiler (source hash: {this.inputHash})
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
");

            writer.AppendLine("using System;");
            writer.AppendLine("using System.Collections.Generic;");
            writer.AppendLine("using System.Linq;");
            writer.AppendLine("using System.Runtime.CompilerServices;");
            writer.AppendLine("using FlatSharp;");
            writer.AppendLine("using FlatSharp.Attributes;");

            foreach (var child in this.Children.Values)
            {
                child.WriteCode(writer, pass, precompiledSerailizers);
            }
        }
Exemplo n.º 2
0
        protected override void OnWriteCode(CodeWriter writer, CodeWritingPass pass, string forFile, IReadOnlyDictionary <string, string> precompiledSerailizers)
        {
            this.AssignIndexes();

            string attribute = this.IsTable ? "[FlatBufferTable]" : "[FlatBufferStruct]";

            writer.AppendLine(attribute);
            writer.AppendLine("[System.Runtime.CompilerServices.CompilerGenerated]");
            writer.AppendLine($"public partial class {this.Name} : object");
            writer.AppendLine($"{{");

            using (writer.IncreaseIndent())
            {
                writer.AppendLine($"partial void OnInitialized();");

                // default ctor.
                string obsolete = this.ObsoleteDefaultConstructor ? $"[Obsolete]" : string.Empty;
                writer.AppendLine($"{obsolete} public {this.Name}() {{ this.OnInitialized(); }}");

                writer.AppendLine($"public {this.Name}({this.Name} source)");
                using (writer.WithBlock())
                {
                    foreach (var field in this.Fields)
                    {
                        field.WriteCopyConstructorLine(writer, "source", this);
                    }

                    writer.AppendLine("this.OnInitialized();");
                }

                foreach (var field in this.Fields)
                {
                    if (!this.IsTable && field.Deprecated)
                    {
                        ErrorContext.Current?.RegisterError($"FlatBuffer structs may not have deprecated fields.");
                    }

                    field.WriteField(writer, this);
                }

                if (pass == CodeWritingPass.SecondPass && precompiledSerailizers != null && this.RequestedSerializer != null)
                {
                    if (precompiledSerailizers.TryGetValue(this.FullName, out string serializer))
                    {
                        writer.AppendLine($"public static ISerializer<{this.FullName}> Serializer {{ get; }} = new {RoslynSerializerGenerator.GeneratedSerializerClassName}().AsISerializer();");
                        writer.AppendLine(string.Empty);
                        writer.AppendLine($"#region Serializer for {this.FullName}");
                        writer.AppendLine(serializer);
                        writer.AppendLine($"#endregion");
                    }
                    else
                    {
                        ErrorContext.Current.RegisterError($"Table {this.FullName} requested serializer, but none was found.");
                    }
                }
            }

            writer.AppendLine($"}}");
        }
Exemplo n.º 3
0
 protected override void OnWriteCode(CodeWriter writer, CodeWritingPass pass, string forFile, IReadOnlyDictionary <string, string> precompiledSerailizers)
 {
     writer.AppendLine($"namespace {this.Name}");
     writer.AppendLine($"{{");
     using (writer.IncreaseIndent())
     {
         foreach (var type in this.Children.Values)
         {
             type.WriteCode(writer, pass, forFile, precompiledSerailizers);
         }
     }
     writer.AppendLine($"}}");
 }
Exemplo n.º 4
0
 protected override void OnWriteCode(CodeWriter writer, CodeWritingPass pass, IReadOnlyDictionary <string, string> precompiledSerailizers)
 {
     writer.AppendLine($"[FlatBufferEnum(typeof({this.ClrUnderlyingType}))]");
     writer.AppendLine("[System.Runtime.CompilerServices.CompilerGenerated]");
     writer.AppendLine($"public enum {this.Name} : {this.ClrUnderlyingType}");
     writer.AppendLine($"{{");
     using (writer.IncreaseIndent())
     {
         foreach (var item in this.nameValuePairs)
         {
             writer.AppendLine($"{item.Key} = {item.Value},");
         }
     }
     writer.AppendLine($"}}");
     writer.AppendLine(string.Empty);
 }
Exemplo n.º 5
0
 public void WriteCode(
     CodeWriter writer,
     CodeWritingPass pass,
     string forFile,
     IReadOnlyDictionary <string, string> precompiledSerailizers)
 {
     // First pass: write all definitions (even from other files)
     //  the first pass is internal and is intended to produce a large
     //  file full of type definitions that FlatSharp can use to build serializers.
     // Second pass: only write things for the target file.
     //   the second pass generates only files in the root fbs file.
     if (pass == CodeWritingPass.FirstPass || forFile == this.DeclaringFile)
     {
         ErrorContext.Current.WithScope(
             this.Name, () => this.OnWriteCode(writer, pass, forFile, precompiledSerailizers));
     }
 }
Exemplo n.º 6
0
 protected virtual void OnWriteCode(CodeWriter writer, CodeWritingPass pass, string forFile, IReadOnlyDictionary <string, string> precompiledSerializer)
 {
 }
Exemplo n.º 7
0
 public void WriteCode(CodeWriter writer, CodeWritingPass pass, IReadOnlyDictionary <string, string> precompiledSerailizers)
 {
     ErrorContext.Current.WithScope(
         this.Name, () => this.OnWriteCode(writer, pass, precompiledSerailizers));
 }
Exemplo n.º 8
0
 protected override void OnWriteCode(CodeWriter writer, CodeWritingPass pass, string forFile, IReadOnlyDictionary <string, string> precompiledSerailizers)
 {
     throw new System.NotImplementedException();
 }