Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/FlatSharp.Compiler/CompilerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@ public record CompilerOptions

[Option("nullable-warnings", Default = false)]
public bool? NullableWarnings { get; set; }

[Option('s', "offset-size")]
public int? OffsetSize { get; set; }

[Option( "file-identifier-size")]
public int? FileIdentifierSize { get; set; }

[Option("strict-file-identifier-size")]
public bool? StrictFileIdentifierSize { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/FlatSharp.Compiler/FlatSharp.Compiler.targets
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<Message Text="$(TargetFramework) $(TargetFrameworkIdentifier): $(CompilerInvocation) --nullable-annotations $(FlatSharpNullable) --input &quot;%(FlatSharpSchema.fullpath)&quot; --output $(IntermediateOutputPath)" Importance="high" />
<Exec
Command="$(CompilerInvocation) --nullable-warnings $(FlatSharpNullable) --input &quot;%(FlatSharpSchema.fullpath)&quot; --output $(IntermediateOutputPath) "
Command="$(CompilerInvocation) $(FlatSharpCompilerOptions) --nullable-warnings $(FlatSharpNullable) --input &quot;%(FlatSharpSchema.fullpath)&quot; --output $(IntermediateOutputPath) "
CustomErrorRegularExpression=".*"
Condition=" '%(FlatSharpSchema.fullpath)' != '' " />

Expand Down
21 changes: 19 additions & 2 deletions src/FlatSharp.Compiler/FlatSharpCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,32 @@ private static void CreateCSharp(

writer = new CodeWriter();

TypeModelContainer container;

if (localOptions.OffsetSize == null
&& localOptions.FileIdentifierSize == null
&& localOptions.StrictFileIdentifierSize == null)
{
container = TypeModelContainer.CreateDefault();
}
else
{
container = TypeModelContainer.CreateDefault(new(
localOptions.OffsetSize,
localOptions.FileIdentifierSize,
localOptions.StrictFileIdentifierSize
));
}

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

ErrorContext.Current.ThrowIfHasErrors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TableOrStructDefinition(

public DefaultConstructorKind? DefaultConstructorKind { get; set; }

public string? FileIdentifier { get; set; }
public string? FileIdentifier;

public FlatBufferDeserializationOption? RequestedSerializer { get; set; }

Expand Down Expand Up @@ -158,6 +158,7 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)

if (this.IsTable && !string.IsNullOrEmpty(this.FileIdentifier))
{
context.TypeModelContainer.OffsetModel.ValidateFileIdentifier(ref this.FileIdentifier);
attributeParts.Add($"{nameof(FlatBufferTableAttribute.FileIdentifier)} = \"{this.FileIdentifier}\"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public class FlatBufferTableAttribute : Attribute
/// <summary>
/// Specifies the file identifier for serialized tables. Must be precisely 4 ASCII characters.
/// </summary>
public string? FileIdentifier { get; set; }
public string? FileIdentifier;
}
}
2 changes: 1 addition & 1 deletion src/FlatSharp/FlatBufferSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public FlatBufferSerializer()
/// Creates a new FlatBufferSerializer using the given options.
/// </summary>
public FlatBufferSerializer(FlatBufferSerializerOptions options)
: this(options, TypeModelContainer.CreateDefault())
: this(options, TypeModelContainer.CreateDefault(new(options.OffsetSize, options.FileIdentifierSize, options.StrictFileIdentifierSize)))
{
}

Expand Down
15 changes: 15 additions & 0 deletions src/FlatSharp/FlatBufferSerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,20 @@ public FlatBufferSerializerOptions(
/// Indicates if "protected internal" modifiers should be converted to protected.
/// </summary>
internal bool ConvertProtectedInternalToProtected { get; set; } = true;

/// <summary>
/// Specify the offset size
/// </summary>
public int? OffsetSize { get; set; } = null;

/// <summary>
/// Specify the size of the file identifier.
/// </summary>
public int? FileIdentifierSize { get; set; } = null;

/// <summary>
/// Specify if the size of the file identifier is strict.
/// </summary>
public bool? StrictFileIdentifierSize { get; set; } = null;
}
}
4 changes: 2 additions & 2 deletions src/FlatSharp/FlatSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
<PackageReference Include="Microsoft.Net.Compilers" Version="3.8.0">
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.11.0" />
<PackageReference Include="Microsoft.Net.Compilers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime</IncludeAssets>
</PackageReference>
Expand Down
83 changes: 83 additions & 0 deletions src/FlatSharp/OffsetModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2020 James Courtney
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace FlatSharp.TypeModel
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

/// <summary>
/// Common or shared type model properties.
/// </summary>
public class OffsetModel
{
public static readonly OffsetModel Default = new(4);

public int OffsetSize;
public int FileIdentifierSize;
public bool StrictFileIdentifierSize;

public OffsetModel(int offsetSize, int fileIdentifierSize, bool strictFileIdentifierSize)
{
OffsetSize = offsetSize;
FileIdentifierSize = fileIdentifierSize;
StrictFileIdentifierSize = strictFileIdentifierSize;
}

public OffsetModel(int offsetSize, int fileIdentifierSize) : this(offsetSize, fileIdentifierSize, fileIdentifierSize == 4)
{
}

public OffsetModel(int offsetSize) : this(offsetSize, offsetSize, offsetSize == 4)
{
}

public OffsetModel(int? offsetSize, int? fileIdentifierSize, bool? strictFileIdentifierSize)
{
OffsetSize = offsetSize ?? 4;
FileIdentifierSize = fileIdentifierSize ?? OffsetSize;
StrictFileIdentifierSize = strictFileIdentifierSize ?? FileIdentifierSize == 4;
}

public void ValidateFileIdentifier(ref string? fileIdentifier)
{
if (string.IsNullOrEmpty(fileIdentifier)) return;

if (fileIdentifier.Length != FileIdentifierSize)
{
if (StrictFileIdentifierSize)
{
throw new InvalidFlatBufferDefinitionException(
$"File identifier '{fileIdentifier}' is invalid. FileIdentifiers must be exactly {FileIdentifierSize} ASCII characters.");
}

fileIdentifier = fileIdentifier.Length > FileIdentifierSize
? fileIdentifier.Substring(0, FileIdentifierSize)
: fileIdentifier.PadRight(FileIdentifierSize, '\0');
}

foreach (var c in fileIdentifier)
{
if (c < 128) continue;

throw new InvalidFlatBufferDefinitionException($"File identifier '{fileIdentifier}' contains non-ASCII characters. Character '{c}' is invalid.");
}
}
}
}
2 changes: 1 addition & 1 deletion src/FlatSharp/TypeModel/EnumTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class EnumTypeModel : RuntimeTypeModel
{
private ITypeModel underlyingTypeModel;

internal EnumTypeModel(Type type, TypeModelContainer typeModelContainer) : base(type, typeModelContainer)
internal EnumTypeModel(Type type, TypeModelContainer typeModelContainer) : base(type, typeModelContainer, typeModelContainer.OffsetModel)
{
this.underlyingTypeModel = null!;
}
Expand Down
2 changes: 2 additions & 0 deletions src/FlatSharp/TypeModel/ITypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace FlatSharp.TypeModel
/// </summary>
public interface ITypeModel
{
OffsetModel OffsetModel { get; }

/// <summary>
/// Gets the schema element type that this type model represents. Note that this is not a 1:1 relationship with the type of class. There can
/// be multiple implementations of ITypeModel that satisfy a particular schema type.
Expand Down
3 changes: 3 additions & 0 deletions src/FlatSharp/TypeModel/ItemMemberModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected ItemMemberModel(
var setMethod = propertyInfo.SetMethod;

this.ItemTypeModel = propertyModel;
this.OffsetModel = propertyModel.OffsetModel;
this.PropertyInfo = propertyInfo;
this.Index = attribute.Index;
this.CustomAccessor = propertyInfo.GetFlatBufferMetadataOrNull(FlatBufferMetadataKind.Accessor);
Expand Down Expand Up @@ -102,6 +103,8 @@ protected ItemMemberModel(
}
}
}

public OffsetModel OffsetModel { get; }

private static bool CanBeOverridden(MethodInfo method)
{
Expand Down
2 changes: 1 addition & 1 deletion src/FlatSharp/TypeModel/NullableTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class NullableTypeModel : RuntimeTypeModel
private Type underlyingType;
private ITypeModel underlyingTypeModel;

internal NullableTypeModel(TypeModelContainer container, Type type) : base(type, container)
internal NullableTypeModel(TypeModelContainer container, Type type) : base(type, container, container.OffsetModel)
{
this.underlyingType = null!;
this.underlyingTypeModel = null!;
Expand Down
18 changes: 16 additions & 2 deletions src/FlatSharp/TypeModel/RuntimeTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ public abstract class RuntimeTypeModel : ITypeModel
{
protected readonly TypeModelContainer typeModelContainer;

internal RuntimeTypeModel(Type clrType, TypeModelContainer typeModelContainer)
internal RuntimeTypeModel(Type clrType, TypeModelContainer typeModelContainer, OffsetModel offsetModel)
{
this.ClrType = clrType;
this.typeModelContainer = typeModelContainer;
OffsetModel = offsetModel;
}

/// <summary>
/// The offset model for serialization.
/// </summary>
public OffsetModel OffsetModel { get; }

/// <summary>
/// Initializes this runtime type model instance.
/// </summary>
Expand Down Expand Up @@ -122,7 +128,15 @@ public virtual bool ValidateDefaultValue(object defaultValue)
/// </summary>
internal static ITypeModel CreateFrom(Type type)
{
return TypeModelContainer.CreateDefault().CreateTypeModel(type);
return CreateFrom(TypeModelContainer.CreateDefault(), type);
}

/// <summary>
/// Gets or creates a runtime type model from the given type. This is only used in test cases any more.
/// </summary>
internal static ITypeModel CreateFrom(TypeModelContainer container, Type type)
{
return container.CreateTypeModel(type);
}

public abstract CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeGenContext context);
Expand Down
2 changes: 1 addition & 1 deletion src/FlatSharp/TypeModel/ScalarTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class ScalarTypeModel : RuntimeTypeModel
internal ScalarTypeModel(
TypeModelContainer container,
Type type,
int size) : base(type, container)
int size) : base(type, container, container.OffsetModel)
{
this.size = size;
}
Expand Down
2 changes: 1 addition & 1 deletion src/FlatSharp/TypeModel/SharedStringTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace FlatSharp.TypeModel
/// </summary>
public class SharedStringTypeModel : RuntimeTypeModel, ITypeModel
{
internal SharedStringTypeModel(TypeModelContainer container) : base(typeof(SharedString), container)
internal SharedStringTypeModel(TypeModelContainer container) : base(typeof(SharedString), container, container.OffsetModel)
{
}

Expand Down
3 changes: 1 addition & 2 deletions src/FlatSharp/TypeModel/StringTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ namespace FlatSharp.TypeModel
/// </summary>
public class StringTypeModel : RuntimeTypeModel
{
internal StringTypeModel(TypeModelContainer container) : base(typeof(string), container)
{
internal StringTypeModel(TypeModelContainer container) : base(typeof(string), container, container.OffsetModel) {
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/FlatSharp/TypeModel/StructTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class StructTypeModel : RuntimeTypeModel
private MethodInfo? onDeserializeMethod;
private FlatBufferStructAttribute attribute = null!;

internal StructTypeModel(Type clrType, TypeModelContainer container) : base(clrType, container)
internal StructTypeModel(Type clrType, TypeModelContainer container) : base(clrType, container, container.OffsetModel)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/FlatSharp/TypeModel/TableMemberModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private string CreateSingleWidthReadItemBody(string parseItemMethodName, string
{this.GetNotPresentStatement()}
}}

ushort relativeOffset = buffer.ReadUShort({vtableLocationVariableName} + {4 + (2 * this.Index)});
ushort relativeOffset = buffer.ReadUShort({vtableLocationVariableName} + {OffsetModel.OffsetSize + (2 * this.Index)});
if (relativeOffset == 0)
{{
{this.GetNotPresentStatement()}
Expand All @@ -140,7 +140,7 @@ private string CreateWideReadItemBody(string parseItemMethodName, string bufferV
int idx = this.Index + i;

relativeOffsets.Add($@"
ushort relativeOffset{i} = buffer.ReadUShort({vtableLocationVariableName} + {4 + (2 * idx)});
ushort relativeOffset{i} = buffer.ReadUShort({vtableLocationVariableName} + {OffsetModel.OffsetSize + (2 * idx)});
if (relativeOffset{i} == 0)
{{
{this.GetNotPresentStatement()}
Expand Down
Loading