-
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #758 from EdwardCooke/ec-aot
Static Object Context for Ahead of Time compiling and Trimmed libraries +semver:feature
- Loading branch information
Showing
34 changed files
with
1,537 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
// of the Software, and to permit persons to whom the Software is furnished to do | ||
// so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace YamlDotNet.Analyzers.StaticGenerator | ||
{ | ||
public class ClassObject | ||
{ | ||
public List<IFieldSymbol> FieldSymbols { get; } | ||
public string FullName { get; } | ||
public string GuidSuffix { get; } | ||
public INamedTypeSymbol ModuleSymbol { get; } | ||
public List<IPropertySymbol> PropertySymbols { get; } | ||
public string SanitizedClassName { get; } | ||
public bool IsDictionary { get; } | ||
public bool IsList { get; } | ||
|
||
public ClassObject(string sanitizedClassName, INamedTypeSymbol moduleSymbol, bool isDictionary = false, bool isList = false) | ||
{ | ||
FieldSymbols = new List<IFieldSymbol>(); | ||
PropertySymbols = new List<IPropertySymbol>(); | ||
FullName = moduleSymbol.GetFullName() ?? string.Empty; | ||
GuidSuffix = Guid.NewGuid().ToString("N"); | ||
ModuleSymbol = moduleSymbol; | ||
SanitizedClassName = sanitizedClassName; | ||
IsDictionary = isDictionary; | ||
IsList = isList; | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
YamlDotNet.Analyzers.StaticGenerator/ClassSyntaxReceiver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
// of the Software, and to permit persons to whom the Software is furnished to do | ||
// so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace YamlDotNet.Analyzers.StaticGenerator | ||
{ | ||
public class ClassSyntaxReceiver : ISyntaxContextReceiver | ||
{ | ||
public List<string> Log { get; } = new(); | ||
public Dictionary<string, ClassObject> Classes { get; } = new Dictionary<string, ClassObject>(); | ||
|
||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context) | ||
{ | ||
if (context.Node is ClassDeclarationSyntax classDeclarationSyntax) | ||
{ | ||
var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax)!; | ||
if (classSymbol.GetAttributes().Any()) | ||
{ | ||
if (classSymbol.GetAttributes().Any(attribute => attribute.AttributeClass?.ToDisplayString() == "YamlDotNet.Serialization.YamlSerializableAttribute")) | ||
{ | ||
ClassObject classObject; | ||
var className = SanitizeName(classSymbol.GetFullName()); | ||
if (Classes.ContainsKey(className)) | ||
{ | ||
classObject = Classes[className]; | ||
} | ||
else | ||
{ | ||
classObject = new ClassObject(className, classSymbol); | ||
Classes[className] = classObject; | ||
} | ||
var members = classSymbol.GetMembers(); | ||
foreach (var member in members) | ||
{ | ||
if (member.IsStatic || | ||
member.DeclaredAccessibility != Accessibility.Public || | ||
member.GetAttributes().Any(x => x.AttributeClass!.ToDisplayString() == "YamlDotNet.Serialization.YamlIgnoreAttribute")) | ||
{ | ||
continue; | ||
} | ||
|
||
if (member is IPropertySymbol propertySymbol) | ||
{ | ||
classObject.PropertySymbols.Add(propertySymbol); | ||
CheckForSupportedGeneric(propertySymbol.Type); | ||
} | ||
else if (member is IFieldSymbol fieldSymbol) | ||
{ | ||
classObject.FieldSymbols.Add(fieldSymbol); | ||
CheckForSupportedGeneric(fieldSymbol.Type); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private string SanitizeName(string name) => new string(name.Select(c => char.IsLetterOrDigit(c) ? c : '_').ToArray()); | ||
|
||
private void CheckForSupportedGeneric(ITypeSymbol type) | ||
{ | ||
var typeName = type.GetFullName(); | ||
var sanitizedTypeName = SanitizeName(typeName); | ||
|
||
if (Classes.ContainsKey(sanitizedTypeName)) | ||
{ | ||
return; | ||
} | ||
|
||
if (typeName.StartsWith("System.Collections.Generic.Dictionary")) | ||
{ | ||
Classes.Add(sanitizedTypeName, new ClassObject(sanitizedTypeName, (INamedTypeSymbol)type, true)); | ||
return; | ||
} | ||
|
||
if (typeName.StartsWith("System.Collections.Generic.List")) | ||
{ | ||
Classes.Add(sanitizedTypeName, new ClassObject(sanitizedTypeName, (INamedTypeSymbol)type, false, true)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
// of the Software, and to permit persons to whom the Software is furnished to do | ||
// so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
using System; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace YamlDotNet.Analyzers.StaticGenerator | ||
{ | ||
public abstract class File | ||
{ | ||
private readonly Action<string> _write; | ||
private readonly Action _indent; | ||
private readonly Action _unindent; | ||
|
||
public File(Action<string> write, Action indent, Action unindent, GeneratorExecutionContext context) | ||
{ | ||
_write = write; | ||
_indent = indent; | ||
_unindent = unindent; | ||
} | ||
|
||
public void Write(string text) | ||
{ | ||
_write(text); | ||
} | ||
|
||
public void Indent() | ||
{ | ||
_indent(); | ||
} | ||
|
||
public void UnIndent() | ||
{ | ||
_unindent(); | ||
} | ||
|
||
public abstract void Write(ClassSyntaxReceiver classSyntaxReceiver); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
YamlDotNet.Analyzers.StaticGenerator/ObjectAccessorFileGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
// of the Software, and to permit persons to whom the Software is furnished to do | ||
// so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace YamlDotNet.Analyzers.StaticGenerator | ||
{ | ||
public class ObjectAccessorFileGenerator : File | ||
{ | ||
public ObjectAccessorFileGenerator(Action<string> Write, Action indent, Action unindent, GeneratorExecutionContext context) : base(Write, indent, unindent, context) | ||
{ | ||
} | ||
|
||
public override void Write(ClassSyntaxReceiver classSyntaxReceiver) | ||
{ | ||
foreach (var o in classSyntaxReceiver.Classes) | ||
{ | ||
var classObject = o.Value; | ||
Write($"public class {classObject.SanitizedClassName}_{classObject.GuidSuffix} : YamlDotNet.Serialization.IObjectAccessor"); | ||
Write("{"); Indent(); | ||
|
||
Write("public void Set(string propertyName, object target, object value)"); | ||
Write("{"); Indent(); | ||
if (classObject.FieldSymbols.Count > 0 || classObject.PropertySymbols.Count > 0) | ||
{ | ||
Write($"var v = ({classObject.FullName})target;"); | ||
Write("switch (propertyName)"); | ||
Write("{"); Indent(); | ||
foreach (var field in classObject.FieldSymbols) | ||
{ | ||
if (!field.IsReadOnly) | ||
{ | ||
Write(GetSetter(field.Name, field.Type)); | ||
} | ||
} | ||
foreach (var property in classObject.PropertySymbols) | ||
{ | ||
if (property.SetMethod != null) | ||
{ | ||
Write(GetSetter(property.Name, property.Type)); | ||
} | ||
} | ||
Write("default: throw new ArgumentOutOfRangeException(\"propertyName\", $\"{propertyName} does not exist or is not settable\");"); | ||
UnIndent(); Write("}"); | ||
} | ||
UnIndent(); Write("}"); | ||
|
||
Write("public object Read(string propertyName, object target)"); | ||
Write("{"); Indent(); | ||
Write($"var v = ({classObject.FullName})target;"); | ||
if (classObject.FieldSymbols.Count > 0 || classObject.PropertySymbols.Count > 0) | ||
{ | ||
Write("switch (propertyName)"); | ||
Write("{"); Indent(); | ||
foreach (var field in classObject.FieldSymbols) | ||
{ | ||
Write(GetReader(field.Name)); | ||
} | ||
foreach (var property in classObject.PropertySymbols) | ||
{ | ||
Write(GetReader(property.Name)); | ||
} | ||
UnIndent(); Write("}"); | ||
} | ||
Write("return null;"); | ||
UnIndent(); Write("}"); | ||
UnIndent(); Write("}"); | ||
} | ||
} | ||
|
||
private string GetSetter(string name, ITypeSymbol type) | ||
{ | ||
return $"case \"{name}\": v.{name} = ({type.GetFullName()})value; return;"; | ||
} | ||
|
||
private string GetReader(string name) | ||
{ | ||
return $"case \"{name}\": return v.{name};"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
// of the Software, and to permit persons to whom the Software is furnished to do | ||
// so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
using System; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace YamlDotNet.Analyzers.StaticGenerator | ||
{ | ||
public class StaticContextFile : File | ||
{ | ||
public StaticContextFile(Action<string> write, Action indent, Action unindent, GeneratorExecutionContext context) : base(write, indent, unindent, context) | ||
{ | ||
} | ||
|
||
public override void Write(ClassSyntaxReceiver classSyntaxReceiver) | ||
{ | ||
Write("public partial class StaticContext : YamlDotNet.Serialization.StaticContext"); | ||
Write("{"); Indent(); | ||
Write("public YamlDotNet.Serialization.ObjectFactories.StaticObjectFactory ObjectFactory { get; } = new StaticObjectFactory();"); | ||
Write("public StaticTypeInspector TypeInspector { get; } = new StaticTypeInspector();"); | ||
Write("public override YamlDotNet.Serialization.ObjectFactories.StaticObjectFactory GetFactory() => ObjectFactory;"); | ||
Write("public override YamlDotNet.Serialization.ITypeInspector GetTypeInspector() => TypeInspector;"); | ||
UnIndent(); Write("}"); | ||
} | ||
} | ||
} |
Oops, something went wrong.